Utils { public static String readUntilNull(ByteBuffer buffer) { StringBuilder sb = new StringBuilder(buffer.remaining()); while (buffer.hasRemaining()) { byte b = buffer.get(); if (b == Constants.NULL) { return sb.toString(); } sb.append((char) b); } throw new IllegalArgumentException("End of buffer reached before NULL"); } 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 testReadUntilNull() { ByteBuffer bb = ByteBuffer.allocate(4); byte[] b = new byte[] { 'H', 'D', 'F', Constants.NULL }; bb.put(b); bb.rewind(); assertThat(Utils.readUntilNull(bb), is(equalTo("HDF"))); }
@Test void testReadUntilNullThrowsIfNoNullIsFound() { ByteBuffer bb = ByteBuffer.allocate(3); byte[] b = new byte[] { 'H', 'D', 'F' }; bb.put(b); bb.rewind(); assertThrows(IllegalArgumentException.class, () -> Utils.readUntilNull(bb)); }
@Test void testReadUntilNullThrowsIfNonAlphanumericCharacterIsSeen() { ByteBuffer bb = ByteBuffer.allocate(3); byte[] b = new byte[] { 'H', 'D', ' ' }; bb.put(b); bb.rewind(); assertThrows(IllegalArgumentException.class, () -> Utils.readUntilNull(bb)); }