Utils { public static int readBytesAsUnsignedInt(ByteBuffer buffer, int length) { switch (length) { case 1: return Byte.toUnsignedInt(buffer.get()); case 2: return Short.toUnsignedInt(buffer.getShort()); case 3: return readArbitraryLengthBytesAsUnsignedInt(buffer, length); case 4: int value = buffer.getInt(); if (value < 0) { throw new ArithmeticException("Could not convert to unsigned"); } return value; case 5: case 6: case 7: return readArbitraryLengthBytesAsUnsignedInt(buffer, length); case 8: return Math.toIntExact(buffer.getLong()); default: throw new IllegalArgumentException("Couldn't read " + length + " bytes as int"); } } private  Utils(); static String toHex(long address); static String readUntilNull(ByteBuffer buffer); static boolean validateName(String name); static void seekBufferToNextMultipleOfEight(ByteBuffer bb); static int readBytesAsUnsignedInt(ByteBuffer buffer, int length); static long readBytesAsUnsignedLong(ByteBuffer buffer, int length); static ByteBuffer createSubBuffer(ByteBuffer source, int length); static int bitsToInt(BitSet bits, int start, int length); static int bytesNeededToHoldNumber(long number); static int[] linearIndexToDimensionIndex(int index, int[] dimensions); static int dimensionIndexToLinearIndex(int[] index, int[] dimensions); static int[] chunkIndexToChunkOffset(int chunkIndex, int[] chunkDimensions, int[] datasetDimensions); static int[] stripLeadingIndex(int[] dims);  }
@Test void testReadingSixBytesToInt() { byte[] bytes = new byte[] { 0, 0, 1, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedInt(bb, 6), is(equalTo(65536))); assertThat(bb.position(), is(6)); }
@Test void testReadingOneByteToInt() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedInt(bb, 1), is(equalTo(12))); assertThat(bb.position(), is(1)); }
@Test void testReadingTwoBytesToInt() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedInt(bb, 2), is(equalTo(12))); assertThat(bb.position(), is(2)); }
@Test void testReadingThreeBytesToInt() { byte[] bytes = new byte[] { 0, 0, 1 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedInt(bb, 3), is(equalTo(65536))); assertThat(bb.position(), is(3)); }
@Test void testReadingFourBytesToInt() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedInt(bb, 4), is(equalTo(12))); assertThat(bb.position(), is(4)); }
@Test void testReadingFourBytesToIntThatCantBeUnsigned() { byte[] bytes = new byte[] { -127, -127, -127, -127 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThrows(ArithmeticException.class, () -> Utils.readBytesAsUnsignedInt(bb, 4)); }
@Test void testReadingEightBytesToInt() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedInt(bb, 8), is(equalTo(12))); assertThat(bb.position(), is(8)); }
@Test void testReadingUnsupportedLengthThrows() { byte[] bytes = new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThrows(IllegalArgumentException.class, () -> Utils.readBytesAsUnsignedInt(bb, 9)); }
@Test void testReadingLargeLongThrows() { ByteBuffer bb = ByteBuffer.allocate(8); bb.putLong(Long.MAX_VALUE); bb.rewind(); assertThrows(ArithmeticException.class, () -> Utils.readBytesAsUnsignedInt(bb, 8)); }