StreamUtil { public static void write(final File file, final String data) throws IOException { try (final FileOutputStream os = new FileOutputStream(file)) { os.write(getBytes(data)); } }  static MessageDigest getDigest(final String algorithm); static MessageDigest getSha1Digest(); static MessageDigest updateDigest(
      final MessageDigest messageDigest, final InputStream data); static String digestAsHex(final MessageDigest messageDigest); static int copy(final InputStream input, final OutputStream output); static void write(final File file, final String data); static void write(
      final File file, final InputStream data, final MessageDigest messageDigest); static int read(final InputStream input, final byte[] dst); static int read(
      final InputStream input, final MutableDirectBuffer buffer, final int offset); static int read(final InputStream input, final byte[] dst, final int offset); static byte[] read(final InputStream input); static void write(final DirectBuffer source, final OutputStream destination); static void write(
      final DirectBuffer source, final OutputStream destination, final int offset, final int length); static void writeLong(final OutputStream outputStream, final long longValue); static long readLong(final InputStream inputStream);  }
@Test public void shouldWriteByteArrayBackedDirectBufferToStream() throws Exception { final byte[] value = "foo".getBytes(); final DirectBuffer buffer = new UnsafeBuffer(value); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(buffer.capacity()); StreamUtil.write(buffer, outputStream); assertThat(outputStream.toByteArray()).isEqualTo(value); final int offset = 1; outputStream.reset(); StreamUtil.write(buffer, outputStream, offset, value.length - offset); assertThat(outputStream.toByteArray()) .isEqualTo(Arrays.copyOfRange(value, offset, value.length)); }
@Test public void shouldWriteByteArrayBackedDirectBufferToStreamWithWrapAdjustement() throws Exception { final byte[] value = "foo".getBytes(); final int bufferOffset = 1; final int bufferLength = value.length - bufferOffset; final DirectBuffer buffer = new UnsafeBuffer(value, bufferOffset, bufferLength); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(buffer.capacity()); StreamUtil.write(buffer, outputStream, 0, bufferLength); assertThat(outputStream.toByteArray()) .isEqualTo(Arrays.copyOfRange(value, bufferOffset, value.length)); final int offset = 1; outputStream.reset(); StreamUtil.write(buffer, outputStream, offset, bufferLength - offset); assertThat(outputStream.toByteArray()) .isEqualTo(Arrays.copyOfRange(value, offset, value.length - offset)); }
@Test public void shouldWriteByteBufferBackedDirectBufferToStream() throws Exception { final byte[] value = "foo".getBytes(); final ByteBuffer underlyingBuffer = ByteBuffer.allocateDirect(value.length); final DirectBuffer buffer = new UnsafeBuffer(underlyingBuffer); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(buffer.capacity()); underlyingBuffer.put(value); StreamUtil.write(buffer, outputStream); assertThat(outputStream.toByteArray()).isEqualTo(value); final int offset = 1; outputStream.reset(); StreamUtil.write(buffer, outputStream, offset, value.length - offset); assertThat(outputStream.toByteArray()) .isEqualTo(Arrays.copyOfRange(value, offset, value.length)); }
@Test public void shouldWriteByteBufferBackedDirectBufferToStreamWithWrapAdjustement() throws Exception { final byte[] value = "foo".getBytes(); final int bufferOffset = 1; final int bufferLength = value.length - bufferOffset; final ByteBuffer underlyingBuffer = ByteBuffer.allocateDirect(value.length); final DirectBuffer buffer = new UnsafeBuffer(underlyingBuffer, bufferOffset, bufferLength); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(buffer.capacity()); underlyingBuffer.put(value); StreamUtil.write(buffer, outputStream, 0, bufferLength); assertThat(outputStream.toByteArray()) .isEqualTo(Arrays.copyOfRange(value, bufferOffset, value.length)); final int offset = 1; outputStream.reset(); StreamUtil.write(buffer, outputStream, offset, bufferLength - offset); assertThat(outputStream.toByteArray()) .isEqualTo(Arrays.copyOfRange(value, offset, value.length - 1)); }