private static int write(final OutputStream dos, final Message header, final Message param,
    final ByteBuf cellBlock, final int totalSize) throws IOException {
    // I confirmed toBytes does same as DataOutputStream#writeInt.
    dos.write(Bytes.toBytes(totalSize));
    // This allocates a buffer that is the size of the message internally.
    header.writeDelimitedTo(dos);
    if (param != null) {
      param.writeDelimitedTo(dos);
    }
    if (cellBlock != null) {
      cellBlock.readBytes(dos, cellBlock.readableBytes());
    }
    dos.flush();
    return totalSize;
  }
Write out header, param, and cell block if there is one.
@param dos       Stream to write into
@param header    to write
@param param     to write
@param cellBlock to write
@return Total number of bytes written.
@throws IOException if write action fails
hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/IPCUtil.java