public void sendTo(ByteBuffer out) {
    finishWriting();
    if (out.remaining() < size()) {
      throw new BufferOverflowException();
    }
    if (chunks != null) {
      for (ByteBuffer bb : chunks) {
        int bytesToWrite = bb.remaining();
        if (bytesToWrite > 0) {
          out.put(bb);
          size -= bytesToWrite;
        }
      }
    }
    {
      ByteBuffer bb = buffer;
      int bytesToWrite = bb.remaining();
      if (bytesToWrite > 0) {
        out.put(bb);
        size -= bytesToWrite;
      }
    }
  }
Write the contents of this stream to the byte buffer.

@throws BufferOverflowException if out is not large enough to contain all of our data.
geode-core/src/main/java/org/apache/geode/internal/HeapDataOutputStream.java