public static void copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)
    throws IOException {
    if (out instanceof ByteBufferWriter) {
      ((ByteBufferWriter) out).write(in, offset, length);
    } else if (in.hasArray()) {
      out.write(in.array(), in.arrayOffset() + offset, length);
    } else {
      for (int i = 0; i < length; ++i) {
        out.write(toByte(in, offset + i));
      }
    }
  }
Copy data from a buffer to an output stream. Does not update the position in the buffer.
@param out    the stream to write bytes to
@param in     the buffer to read bytes from
@param offset the offset in the buffer (from the buffer's array offset) to start copying bytes
              from
@param length the number of bytes to copy
hbase-common/src/main/java/org/apache/hadoop/hbase/util/ByteBufferUtils.java