private static void writeToCOS(Message result, Message header, int totalSize, ByteBuffer pbBuf) throws IOException { ByteBufferUtils.putInt(pbBuf, totalSize); // create COS that works on BB CodedOutputStream cos = CodedOutputStream.newInstance(pbBuf); if (header != null) { cos.writeMessageNoTag(header); } if (result != null) { cos.writeMessageNoTag(result); } cos.flush(); cos.checkNoSpaceLeft(); }