@Override
  public long writeTo(WritableByteChannel channel) throws IOException {
    long written = 0;
    if (!isSendComplete()) {
      written = readSet.writeTo(currentWriteIndex, channel,
          sendInfoList.get(currentWriteIndex).relativeOffset() + sizeWrittenFromCurrentIndex,
          sendInfoList.get(currentWriteIndex).sizetoSend() - sizeWrittenFromCurrentIndex);
      logger.trace("writeindex {} relativeOffset {} maxSize {} written {}", currentWriteIndex,
          sendInfoList.get(currentWriteIndex).relativeOffset() + sizeWrittenFromCurrentIndex,
          sendInfoList.get(currentWriteIndex).sizetoSend() - sizeWrittenFromCurrentIndex, written);
      sizeWritten += written;
      sizeWrittenFromCurrentIndex += written;
      logger.trace("size written in this loop : {} size written till now : {}", written, sizeWritten);
      if (sizeWrittenFromCurrentIndex == sendInfoList.get(currentWriteIndex).sizetoSend()) {
        currentWriteIndex++;
        sizeWrittenFromCurrentIndex = 0;
      }
    }
    return written;
  }
Extract the encryption key from the message at the given index from the readSet.
@param readSetIndex the index in the readSet for the message from which the encryption key has to be extracted.
@param encryptionKeyRelativeOffset the relative offset of the encryption key record in the message.
@param encryptionKeySize the size of the encryption key record in the message.
@return the extracted encryption key.
@throws IOException if an IO error is encountered while deserializing the message.
@throws MessageFormatException if a Message Format error is encountered while deserializing the message.
ambry-messageformat/src/main/java/com/github/ambry/messageformat/MessageFormatSend.java