Class GrpcFrames

java.lang.Object
com.codename1.backend.grpc.GrpcFrames

public final class GrpcFrames extends Object

The gRPC length-prefixed message framing: every message on a gRPC stream is one byte of flags (bit 0 set when the message is compressed), a four-byte big-endian length, and that many bytes of payload.

HTTP/2 DATA frames and gRPC messages are unrelated: one DATA frame may hold several messages, and one message may span many DATA frames. GrpcFrames.Decoder is fed whatever arrived and hands back the messages that are complete, which is the only correct way to read a stream.

Rules a decoder must keep, and why:

  • The length is checked against a ceiling before anything is allocated. Four bytes from the peer would otherwise be a 2 GB allocation on demand.
  • A compressed flag with no negotiated encoding is a protocol error (INTERNAL), never a guess at the codec.
  • Any flag bit other than bit 0 is reserved; a set one is an error too.
  • A gzip message is bounded by the same ceiling after inflation, or a small message becomes a decompression bomb.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Reassembles messages from bytes as they arrive.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default largest message accepted, as in grpc-java and grpc-go: 4 MB.
    static final int
    The prefix in front of every message.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    frame(byte[] message)
    Frames one message, uncompressed.
    static byte[]
    frame(byte[] message, boolean gzip)
    Frames one message, gzip-compressed when asked.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PREFIX_LENGTH

      public static final int PREFIX_LENGTH
      The prefix in front of every message.
      See Also:
    • DEFAULT_MAX_MESSAGE

      public static final int DEFAULT_MAX_MESSAGE
      The default largest message accepted, as in grpc-java and grpc-go: 4 MB.
      See Also:
  • Method Details

    • frame

      public static byte[] frame(byte[] message)
      Frames one message, uncompressed.
      Parameters:
      message - the serialized message
      Returns:
      prefix and payload
    • frame

      public static byte[] frame(byte[] message, boolean gzip)
      Frames one message, gzip-compressed when asked.
      Parameters:
      message - the serialized message
      gzip - whether to compress it (only when the peer accepts gzip)
      Returns:
      prefix and payload