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. // The final message may not be completely written because we do not flush the last buffer. // Do not report the last message as sent. sink . deliverFrame ( writeableHeader , false , false , messagesBuffered - 1 ) ; messagesBuffered = 1 ; // 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 , 0 ) ; } // 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 ) ; currentMessageWireSize = messageLength ; }
Write a message that has been serialized to a sequence of buffers .