Skip to content

Commit 9256264

Browse files
committed
Add to unsigned conversations
1 parent beb9398 commit 9256264

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v0.4.4
44

55
* add feature for gathering parts of the array as primitives (e.g. intAt(int position))
6+
* add to unsigned byte conversations
67

78
## v0.4.3
89

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ Accessing part of the array as primitives from arbitrary position:
318318

319319
```java
320320
Bytes.wrap(array).bitAt(4); // 0010 1000 -> false
321-
Bytes.wrap(array).byteAt(14);
321+
Bytes.wrap(array).byteAt(14); // 1111 1111 -> -1
322+
Bytes.wrap(array).unsignedByteAt(14); // 1111 1111 -> 255
322323
Bytes.wrap(array).intAt(4);
323324
Bytes.wrap(array).longAt(6);
324325
```
@@ -420,6 +421,7 @@ To primitives (if the internal array is not too long)
420421

421422
```java
422423
Bytes.wrap(array).toByte();
424+
Bytes.wrap(array).toUnsignedByte();
423425
Bytes.wrap(array).toInt();
424426
Bytes.wrap(array).toDouble();
425427
```

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,7 @@ public int lastIndexOf(byte target) {
960960
* @throws IndexOutOfBoundsException if the {@code bitIndex} argument is negative or not less than the length of this array in bits.
961961
*/
962962
public boolean bitAt(int bitIndex) {
963-
if (bitIndex < 0 || bitIndex > lengthBit()) {
964-
throw new IndexOutOfBoundsException("cannot get bit from index out of bounds: " + bitIndex);
965-
}
963+
Util.checkIndexBounds(lengthBit(), bitIndex, 1, "bit");
966964
return ((byteAt(length() - 1 - (bitIndex / 8)) >>> bitIndex % 8) & 1) != 0;
967965
}
968966

@@ -976,12 +974,24 @@ public boolean bitAt(int bitIndex) {
976974
* @throws IndexOutOfBoundsException if the {@code index} argument is negative or not less than the length of this array.
977975
*/
978976
public byte byteAt(int index) {
979-
if (index < 0 || index > length()) {
980-
throw new IndexOutOfBoundsException("cannot get byte from index out of bounds: " + index);
981-
}
977+
Util.checkIndexBounds(length(), index, 1, "byte");
982978
return internalArray()[index];
983979
}
984980

981+
/**
982+
* Returns the unsigned {@code byte} value at the specified index as an int.
983+
* An index ranges from {@code 0} to {@code length() - 1}. The first {@code char} value of the sequence
984+
* is at index {@code 0}, the next at index {@code 1}, and so on, as for array indexing.
985+
*
986+
* @param index the index of the unsigned {@code byte} value.
987+
* @return the unsigned {@code byte} value at the specified index of the underlying byte array as type 4 byte integer
988+
* @throws IndexOutOfBoundsException if the {@code index} argument is negative or not less than the length of this array.
989+
*/
990+
public int unsignedByteAt(int index) {
991+
Util.checkIndexBounds(length(), index, 1, "unsigned byte");
992+
return 0xff & internalArray()[index];
993+
}
994+
985995
/**
986996
* Returns the {@code char} value at the specified index.
987997
* Reads the primitive from given index and the following byte and interprets it according to byte order.
@@ -1334,7 +1344,7 @@ public BigInteger toBigInteger() {
13341344
}
13351345

13361346
/**
1337-
* If the underlying byte array is exactly 1 byte / 8 bit long, returns unsigned two-complement
1347+
* If the underlying byte array is exactly 1 byte / 8 bit long, returns signed two-complement
13381348
* representation for a Java byte value.
13391349
* <p>
13401350
* If you just want to get the first element as {@code byte}, see {@link #byteAt(int)}, using index zero.
@@ -1348,6 +1358,21 @@ public byte toByte() {
13481358
return internalArray()[0];
13491359
}
13501360

1361+
/**
1362+
* If the underlying byte array is exactly 1 byte / 8 bit long, returns unsigned two-complement
1363+
* representation for a Java byte value wrapped in an 4 byte int.
1364+
* <p>
1365+
* If you just want to get the first element as {@code byte}, see {@link #byteAt(int)}, using index zero.
1366+
*
1367+
* @return the unsigned byte representation wrapped in an int
1368+
* @throws IllegalStateException if byte array has length not equal to 1
1369+
* @see <a href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">Primitive Types</a>
1370+
*/
1371+
public int toUnsignedByte() {
1372+
Util.checkExactLength(length(), 1, "unsigned byte");
1373+
return unsignedByteAt(0);
1374+
}
1375+
13511376
/**
13521377
* If the underlying byte array is exactly 2 byte / 16 bit long, return unsigned two-complement
13531378
* representation for a Java char integer value. The output is dependent on the set {@link #byteOrder()}.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public void bitAt() throws Exception {
190190
public void byteAt() throws Exception {
191191
assertEquals(0, Bytes.allocate(1).byteAt(0));
192192
assertEquals(0, Bytes.allocate(128).byteAt(127));
193+
assertEquals(-1, Bytes.from((byte) 0b1111_1111).byteAt(0));
193194

194195
for (int i = 0; i < example_bytes_twentyfour.length; i++) {
195196
assertEquals(example_bytes_twentyfour[i], Bytes.wrap(example_bytes_twentyfour).byteAt(i));
@@ -208,6 +209,25 @@ public void byteAt() throws Exception {
208209
}
209210
}
210211

212+
@Test
213+
public void unsignedByteAt() throws Exception {
214+
assertEquals(0, Bytes.allocate(1).unsignedByteAt(0));
215+
assertEquals(0, Bytes.allocate(128).unsignedByteAt(127));
216+
assertEquals(255, Bytes.from((byte) 0b1111_1111).unsignedByteAt(0));
217+
218+
try {
219+
Bytes.allocate(1).unsignedByteAt(1);
220+
fail();
221+
} catch (IndexOutOfBoundsException ignored) {
222+
}
223+
224+
try {
225+
Bytes.allocate(16).unsignedByteAt(-1);
226+
fail();
227+
} catch (IndexOutOfBoundsException ignored) {
228+
}
229+
}
230+
211231
@Test
212232
public void charAt() throws Exception {
213233
assertEquals(0, Bytes.allocate(2).charAt(0));

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void toBitSet() throws Exception {
7979
public void toByte() throws Exception {
8080
assertEquals(example_bytes_one[0], Bytes.from(example_bytes_one).toByte());
8181
assertEquals((byte) 0, Bytes.from(new byte[1]).toByte());
82+
assertEquals(-1, Bytes.from((byte) 0b1111_1111).toByte());
8283

8384
try {
8485
Bytes.from(example_bytes_two).toByte();
@@ -87,6 +88,19 @@ public void toByte() throws Exception {
8788
}
8889
}
8990

91+
@Test
92+
public void toUnsignedByte() throws Exception {
93+
assertEquals(example_bytes_one[0], Bytes.from(example_bytes_one).toUnsignedByte());
94+
assertEquals((byte) 0, Bytes.from(new byte[1]).toUnsignedByte());
95+
assertEquals(255, Bytes.from((byte) 0b1111_1111).toUnsignedByte());
96+
97+
try {
98+
Bytes.from(example_bytes_two).toUnsignedByte();
99+
fail();
100+
} catch (IllegalStateException ignored) {
101+
}
102+
}
103+
90104
@Test
91105
public void toChar() throws Exception {
92106
assertEquals(6767, Bytes.from(example_bytes_two).toChar());

0 commit comments

Comments
 (0)