Utils { public static long readBytesAsUnsignedLong(ByteBuffer buffer, int length) { switch (length) { case 1: return Byte.toUnsignedLong(buffer.get()); case 2: return Short.toUnsignedLong(buffer.getShort()); case 3: return readArbitraryLengthBytesAsUnsignedLong(buffer, length); case 4: return Integer.toUnsignedLong(buffer.getInt()); case 5: case 6: case 7: return readArbitraryLengthBytesAsUnsignedLong(buffer, length); case 8: long value = buffer.getLong(); if (value < 0 && value != Constants.UNDEFINED_ADDRESS) { throw new ArithmeticException("Could not convert to unsigned"); } return value; 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 testReadingOneByteToLong() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 1), is(equalTo(12L))); assertThat(bb.position(), is(1)); }
@Test void testReadingTwoBytesToLong() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 2), is(equalTo(12L))); assertThat(bb.position(), is(2)); }
@Test void testReadingThreeBytesToLong() { byte[] bytes = new byte[] { 0, 0, 1 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 3), is(equalTo(65536L))); assertThat(bb.position(), is(3)); }
@Test void testReadingThreeBytesToLongBigEndian() { byte[] bytes = new byte[] { 1, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(BIG_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 3), is(equalTo(65536L))); assertThat(bb.position(), is(3)); }
@Test void testReadingFourBytesToLong() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 4), is(equalTo(12L))); assertThat(bb.position(), is(4)); }
@Test void testReadingFiveBytesToLong() { byte[] bytes = new byte[] { 0, 0, 0, 0, 1 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 5), is(equalTo(4294967296L))); assertThat(bb.position(), is(5)); }
@Test void testReadingSixBytesToLong() { byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 1 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 6), is(equalTo(1099511627776L))); assertThat(bb.position(), is(6)); }
@Test void testReadingSevenBytesToLong() { byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 1 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 7), is(equalTo(281474976710656L))); assertThat(bb.position(), is(7)); }
@Test void testReadingEightBytesToLong() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThat(Utils.readBytesAsUnsignedLong(bb, 8), is(equalTo(12L))); assertThat(bb.position(), is(8)); }
@Test void testReadingUnsupportedLengthLongThrows() { byte[] bytes = new byte[] { 12, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(bytes).order(LITTLE_ENDIAN); assertThrows(IllegalArgumentException.class, () -> Utils.readBytesAsUnsignedLong(bb, 9)); }
@Test void testReadingEightByteNegativeIntegerThrows() { ByteBuffer bb = ByteBuffer.allocate(8); bb.rewind(); bb.putInt(-462); bb.rewind(); assertThrows(ArithmeticException.class, () -> Utils.readBytesAsUnsignedLong(bb, 8)); }
@Test void testReadingEightByteNegativeLongThrows() { ByteBuffer bb = ByteBuffer.allocate(8); bb.rewind(); bb.putLong(-462); bb.rewind(); assertThrows(ArithmeticException.class, () -> Utils.readBytesAsUnsignedLong(bb, 8)); }
@Test void testReadingUndefinedAddressSpecialCaseWorks() { ByteBuffer bb = ByteBuffer.allocate(8); bb.putLong(Constants.UNDEFINED_ADDRESS); bb.rewind(); assertThat(Utils.readBytesAsUnsignedLong(bb, 8), is(equalTo(Constants.UNDEFINED_ADDRESS))); }