BufferUtil { public static byte[] bufferAsArray(final DirectBuffer buffer) { final byte[] array; array = new byte[buffer.capacity()]; buffer.getBytes(0, array); return array; } private  BufferUtil(); static String bufferAsString(final DirectBuffer buffer); static String bufferAsString(
      final DirectBuffer buffer, final int offset, final int length); static DirectBuffer wrapString(final String argument); static boolean equals(final DirectBuffer buffer1, final DirectBuffer buffer2); static boolean contentsEqual(final DirectBuffer buffer1, final DirectBuffer buffer2); static DirectBuffer cloneBuffer(final DirectBuffer src); static DirectBuffer cloneBuffer(
      final DirectBuffer src, final int offset, final int length); static String bufferAsHexString(final BufferWriter writer); static String bufferAsHexString(final BufferWriter writer, final int wrap); static String bufferAsHexString(final DirectBuffer buffer); static String bufferAsHexString(final DirectBuffer buffer, final int wrap); static String bufferAsHexString(
      final DirectBuffer buffer, final int offset, final int length); static String bufferAsHexString(
      final DirectBuffer buffer, final int offset, final int length, final int wrap); static String bytesAsHexString(final byte[] bytes); static String bytesAsHexString(final byte[] bytes, final int wrap); static byte[] bufferAsArray(final DirectBuffer buffer); static MutableDirectBuffer wrapArray(final byte[] array); static MutableDirectBuffer wrapBytes(final int... bytes); static int bufferContentsHash(final DirectBuffer buffer); static boolean startsWith(
      final byte[] prefix,
      final int prefixOffset,
      final int prefixLength,
      final byte[] content,
      int contentOffset,
      final int contentLength); static final int NO_WRAP; static final int DEFAULT_WRAP; }
@Test public void shouldReturnCopyOfByteArrayWhenWrappingPartialArray() { final byte[] expected = new byte[BYTES1.length - 1]; final DirectBuffer buffer = new UnsafeBuffer(); buffer.wrap(BYTES1, 1, BYTES1.length - 1); final byte[] bytes = BufferUtil.bufferAsArray(buffer); System.arraycopy(BYTES1, 1, expected, 0, expected.length); assertThat(buffer.byteArray()).isEqualTo(BYTES1); assertThat(bytes).isEqualTo(expected); assertThat(bytes).isNotEqualTo(BYTES1); }
@Test public void shouldReturnNewByteArrayWhenNotWrappingByteArray() { final MutableDirectBuffer buffer = new ExpandableDirectByteBuffer(); buffer.putBytes(0, BYTES1); final byte[] bytes = BufferUtil.bufferAsArray(buffer); assertThat(buffer.byteArray()).isNull(); for (int i = 0; i < BYTES1.length; i++) { assertThat(bytes[i]).isEqualTo(buffer.getByte(i)); } }
@Test public void shouldNotReturnWrappedArrayIfBufferOffsetIsZero() { final DirectBuffer buffer = new UnsafeBuffer(BYTES1); final byte[] bytes = BufferUtil.bufferAsArray(buffer); assertThat(buffer.byteArray()).isEqualTo(BYTES1); assertThat(bytes).isNotSameAs(BYTES1); }