Skip to content

Commit 7aa1c2d

Browse files
committed
Adds iterable interface
1 parent 5412b4a commit 7aa1c2d

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* add check method if transformer supports inplace
66
* adds contains method
7+
* adds Iterable interface
78

89
## v0.4.1
910

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ Methods that return additional information about the instance.
288288
Finding occurrence of specific bytes:
289289

290290
```java
291+
Bytes.wrap(array).contains((byte) 0xE1);
291292
Bytes.wrap(array).indexOf((byte) 0xFD);
292293
Bytes.wrap(array).lastIndexOf((byte) 0xAE);
293294
```
@@ -319,6 +320,15 @@ The `toString()` methods only shows the length and a preview of maximal 8 bytes:
319320
16 bytes (0x7ed1fdaa...12af000a)
320321
```
321322

323+
Bytes also implements the `Iterable` interface, so it can be used in a
324+
foreach loop:
325+
326+
```java
327+
for (Byte aByte : bytesInstance) {
328+
...
329+
}
330+
```
331+
322332
### Validation
323333

324334
A simple validation framework which can be used to check the internal byte array:

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.ByteArrayInputStream;
2525
import java.io.File;
2626
import java.io.InputStream;
27+
import java.io.Serializable;
2728
import java.math.BigInteger;
2829
import java.nio.ByteBuffer;
2930
import java.nio.ByteOrder;
@@ -59,7 +60,7 @@
5960
* </pre>
6061
*/
6162
@SuppressWarnings("WeakerAccess")
62-
public class Bytes implements Comparable<Bytes>, AbstractBytes {
63+
public class Bytes implements Comparable<Bytes>, AbstractBytes, Serializable, Iterable<Byte> {
6364

6465
/* FACTORY ***************************************************************************************************/
6566

@@ -1387,6 +1388,11 @@ public String toString() {
13871388
return length() + " bytes " + preview;
13881389
}
13891390

1391+
@Override
1392+
public Iterator<Byte> iterator() {
1393+
return new Util.BytesIterator(internalArray());
1394+
}
1395+
13901396
/**
13911397
* Internal factory for {@link Bytes} instances
13921398
*/
@@ -1397,4 +1403,5 @@ public Bytes wrap(byte[] array, ByteOrder byteOrder) {
13971403
}
13981404
}
13991405

1406+
static final long serialVersionUID = 1L;
14001407
}

src/main/java/at/favre/lib/bytes/Util.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ static byte[] readFromFile(File file) {
322322
limitations under the License.
323323
=================================================================================================
324324
*/
325-
public static final class Entropy<T> {
325+
static final class Entropy<T> {
326326
private final Map<T, Integer> map = new HashMap<>();
327327
private int total = 0;
328328

@@ -349,4 +349,41 @@ public double entropy() {
349349
return entropy;
350350
}
351351
}
352+
353+
/**
354+
* A simple iterator for the bytes class, which does not support remove
355+
*/
356+
static final class BytesIterator implements Iterator<Byte> {
357+
private final byte[] array;
358+
/**
359+
* Index of element to be returned by subsequent call to next.
360+
*/
361+
private int cursor = 0;
362+
363+
BytesIterator(byte[] array) {
364+
this.array = array;
365+
}
366+
367+
@Override
368+
public boolean hasNext() {
369+
return cursor != array.length;
370+
}
371+
372+
@Override
373+
public Byte next() {
374+
try {
375+
int i = cursor;
376+
Byte next = array[i];
377+
cursor = i + 1;
378+
return next;
379+
} catch (IndexOutOfBoundsException e) {
380+
throw new NoSuchElementException();
381+
}
382+
}
383+
384+
@Override
385+
public void remove() {
386+
throw new UnsupportedOperationException("The Bytes iterator does not support removing");
387+
}
388+
}
352389
}

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.nio.ByteOrder;
2727
import java.nio.ReadOnlyBufferException;
28+
import java.util.NoSuchElementException;
2829

2930
import static org.junit.Assert.*;
3031

@@ -254,4 +255,25 @@ public void readOnly() throws Exception {
254255
Bytes b = Bytes.from(example_bytes_twentyfour).readOnly();
255256
assertSame(b, b.readOnly());
256257
}
258+
259+
@Test
260+
public void iteratorTest() throws Exception {
261+
Bytes b = Bytes.wrap(example_bytes_seven);
262+
263+
int counter = 0;
264+
for (Byte aByte : b) {
265+
assertEquals((Byte) example_bytes_seven[counter], aByte);
266+
counter++;
267+
}
268+
}
269+
270+
@Test(expected = UnsupportedOperationException.class)
271+
public void iteratorTestRemoveNotPossible() throws Exception {
272+
Bytes.wrap(example_bytes_seven).iterator().remove();
273+
}
274+
275+
@Test(expected = NoSuchElementException.class)
276+
public void iteratorNoElement() throws Exception {
277+
Bytes.allocate(0).iterator().next();
278+
}
257279
}

0 commit comments

Comments
 (0)