public static void writeFully(OutputStream outputStream, ByteBuffer buffer) throws IOException {
    if (!buffer.hasRemaining()) {
      return;
    }
    byte[] chunk = new byte[WRITE_CHUNK_SIZE];
    while (buffer.hasRemaining()) {
      int chunkSize = Math.min(chunk.length, buffer.remaining());
      buffer.get(chunk, 0, chunkSize);
      outputStream.write(chunk, 0, chunkSize);
    }
  }
@Test
  public void testWriteFully() throws Exception {
    byte[] input = Strings.repeat("Welcome to Warsaw!\n", 12345).getBytes(StandardCharsets.UTF_8);
    InMemoryOutputFile outputFile = new InMemoryOutputFile();
    try (PositionOutputStream outputStream = outputFile.create()) {
      IOUtil.writeFully(outputStream, ByteBuffer.wrap(input.clone()));
    }
    assertThat(outputFile.toByteArray()).isEqualTo(input);
  }