code: public static int flush(final BufferByteOutput<?> output, final WritableByteChannel channel) throws IOException {
          final ByteBuffer buffer = output.getTarget();
          if (buffer == null) {
              return 0;
          }
          int written = 0;
          for (buffer.flip(); buffer.hasRemaining(); ) {
              written += channel.write(buffer);
          }
          buffer.clear(); // position -> zero; limit -> capacity
          return written;
      }
docs: Flushes the internal byte buffer of given byte output to specified channel and returns the number of bytes
  written.
  
  @param output  the output whose buffer is flushed.
  @param channel the channel to which the buffer is flushed.
  @return the number of bytes written while flushing; {@code 0} if there is no inter buffer.
  @throws IOException if an I/O error occurs.