Class MessageFramer

  • All Implemented Interfaces:
    Framer

    public class MessageFramer
    extends java.lang.Object
    implements Framer
    Encodes gRPC messages to be delivered via the transport layer which implements MessageFramer.Sink.
    • Field Detail

      • NO_MAX_OUTBOUND_MESSAGE_SIZE

        private static final int NO_MAX_OUTBOUND_MESSAGE_SIZE
        See Also:
        Constant Field Values
      • maxOutboundMessageSize

        private int maxOutboundMessageSize
      • messageCompression

        private boolean messageCompression
      • headerScratch

        private final java.nio.ByteBuffer headerScratch
      • closed

        private boolean closed
      • messagesBuffered

        private int messagesBuffered
      • currentMessageSeqNo

        private int currentMessageSeqNo
      • currentMessageWireSize

        private long currentMessageWireSize
    • Constructor Detail

      • MessageFramer

        public MessageFramer​(MessageFramer.Sink sink,
                             WritableBufferAllocator bufferAllocator,
                             StatsTraceContext statsTraceCtx)
        Creates a MessageFramer.
        Parameters:
        sink - the sink used to deliver frames to the transport
        bufferAllocator - allocates buffers that the transport can commit to the wire.
    • Method Detail

      • setMaxOutboundMessageSize

        public void setMaxOutboundMessageSize​(int maxSize)
        Description copied from interface: Framer
        Set a size limit for each outbound message.
        Specified by:
        setMaxOutboundMessageSize in interface Framer
      • writePayload

        public void writePayload​(java.io.InputStream message)
        Writes out a payload message.
        Specified by:
        writePayload in interface Framer
        Parameters:
        message - contains the message to be written out. It will be completely consumed.
      • writeUncompressed

        private int writeUncompressed​(java.io.InputStream message,
                                      int messageLength)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • writeCompressed

        private int writeCompressed​(java.io.InputStream message,
                                    int unusedMessageLength)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • getKnownLength

        private int getKnownLength​(java.io.InputStream inputStream)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • writeKnownLengthUncompressed

        private int writeKnownLengthUncompressed​(java.io.InputStream message,
                                                 int messageLength)
                                          throws java.io.IOException
        Write an unserialized message with a known length, uncompressed.
        Throws:
        java.io.IOException
      • writeBufferChain

        private void writeBufferChain​(MessageFramer.BufferChainOutputStream bufferChain,
                                      boolean compressed)
        Write a message that has been serialized to a sequence of buffers.
      • writeToOutputStream

        private static int writeToOutputStream​(java.io.InputStream message,
                                               java.io.OutputStream outputStream)
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • writeRaw

        private void writeRaw​(byte[] b,
                              int off,
                              int len)
      • flush

        public void flush()
        Flushes any buffered data in the framer to the sink.
        Specified by:
        flush in interface Framer
      • isClosed

        public boolean isClosed()
        Indicates whether or not this framer has been closed via a call to either close() or dispose().
        Specified by:
        isClosed in interface Framer
      • close

        public void close()
        Flushes and closes the framer and releases any buffers. After the framer is closed or disposed, additional calls to this method will have no affect.
        Specified by:
        close in interface Framer
      • dispose

        public void dispose()
        Closes the framer and releases any buffers, but does not flush. After the framer is closed or disposed, additional calls to this method will have no affect.
        Specified by:
        dispose in interface Framer
      • releaseBuffer

        private void releaseBuffer()
      • commitToSink

        private void commitToSink​(boolean endOfStream,
                                  boolean flush)
      • verifyNotClosed

        private void verifyNotClosed()