/** Write a message that has been serialized to a sequence of buffers. */ private void writeBufferChain(BufferChainOutputStream bufferChain, boolean compressed) { ByteBuffer header = ByteBuffer.wrap(headerScratch); header.put(compressed ? COMPRESSED : UNCOMPRESSED); int messageLength = bufferChain.readableBytes(); header.putInt(messageLength); WritableBuffer writeableHeader = bufferAllocator.allocate(HEADER_LENGTH); writeableHeader.write(headerScratch, 0, header.position()); if (messageLength == 0) { // the payload had 0 length so make the header the current buffer. buffer = writeableHeader; return; } // Note that we are always delivering a small message to the transport here which // may incur transport framing overhead as it may be sent separately to the contents // of the GRPC frame. sink.deliverFrame(writeableHeader, false, false); // Commit all except the last buffer to the sink List<WritableBuffer> bufferList = bufferChain.bufferList; for (int i = 0; i < bufferList.size() - 1; i++) { sink.deliverFrame(bufferList.get(i), false, false); } // Assign the current buffer to the last in the chain so it can be used // for future writes or written with end-of-stream=true on close. buffer = bufferList.get(bufferList.size() - 1); }
private void commitToSink(boolean endOfStream, boolean flush) { WritableBuffer buf = buffer; buffer = null; sink.deliverFrame(buf, endOfStream, flush); }