@Override
  public long writeTo(WritableByteChannel channel) throws IOException {
    if (bufferToSend == null) {
      prepareBuffer();
    }
    if (nioBuffers == null) {
      nioBuffers = bufferToSend.nioBuffers();
    }
    long totalWritten = 0;
    if (bufferToSend.readableBytes() != 0) {
      int currentWritten = -1;
      while (bufferIndex < nioBuffers.length && currentWritten != 0) {
        currentWritten = -1;
        ByteBuffer byteBuffer = nioBuffers[bufferIndex];
        if (!byteBuffer.hasRemaining()) {
          // some bytebuffers are zero length, ignore those bytebuffers.
          bufferIndex++;
        } else {
          currentWritten = channel.write(byteBuffer);
          totalWritten += currentWritten;
        }
      }
      bufferToSend.skipBytes((int) totalWritten);
      sentBytes += totalWritten;
    }
    if (sentBytes >= bufferToSend.readableBytes() && toSend != null && !toSend.isSendComplete()) {
      long written = toSend.writeTo(channel);
      totalWritten += written;
      sentBytes += written;
    }
    return totalWritten;
  }
A private method shared by {@link GetResponse#writeTo(WritableByteChannel)} and
{@link GetResponse#writeTo(AsyncWritableChannel, Callback)}.
This method allocate bufferToSend and write metadata to it if bufferToSend is null.
ambry-protocol/src/main/java/com/github/ambry/protocol/GetResponse.java