@@ -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()}.
0 commit comments