Utils { public static int bytesNeededToHoldNumber(long number) { if (number < 0) { throw new IllegalArgumentException("Only for unsigned numbers"); } if (number == 0) { return 1; } return (int) Math.ceil(BigInteger.valueOf(number).bitLength() / 8.0); } 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 testBytesNeededToHoldNumber() { assertThat(Utils.bytesNeededToHoldNumber(0), is(equalTo(1))); assertThat(Utils.bytesNeededToHoldNumber(1), is(equalTo(1))); assertThat(Utils.bytesNeededToHoldNumber(2), is(equalTo(1))); assertThat(Utils.bytesNeededToHoldNumber(255), is(equalTo(1))); assertThat(Utils.bytesNeededToHoldNumber(256), is(equalTo(2))); assertThat(Utils.bytesNeededToHoldNumber(65535), is(equalTo(2))); assertThat(Utils.bytesNeededToHoldNumber(65536), is(equalTo(3))); }
@Test void testBytesNeededToHoldNumberThrowsWithNegative() { assertThrows(IllegalArgumentException.class, () -> Utils.bytesNeededToHoldNumber(-123)); }