public int sendTo(SocketChannel chan) throws IOException {
    finishWriting();
    if (size() == 0) {
      return 0;
    }
    int result;
    if (chunks != null) {
      ByteBuffer[] bufs = new ByteBuffer[chunks.size() + 1];
      bufs = chunks.toArray(bufs);
      bufs[chunks.size()] = buffer;
      result = (int) chan.write(bufs);
    } else {
      result = chan.write(buffer);
    }
    size -= result;
    return result;
  }
Write this stream to the specified channel. Call multiple times until size returns zero to make
sure all bytes in the stream have been written.

@return the number of bytes written, possibly zero.
@throws IOException if channel is closed, not yet connected, or some other I/O error occurs.
geode-core/src/main/java/org/apache/geode/internal/HeapDataOutputStream.java