Class IncrementalGrpcFrameDecoder

java.lang.Object
org.mockserver.grpc.IncrementalGrpcFrameDecoder

public class IncrementalGrpcFrameDecoder extends Object
Incremental gRPC length-prefixed frame decoder that accumulates across multiple feed() calls, retaining any trailing partial bytes for the next call.

Unlike GrpcFrameCodec.decode(byte[]), which operates on a complete buffer and discards trailing partial frames (line ~49), this decoder preserves partial frames across feeds so that a gRPC message split across multiple HTTP/2 DATA frames is reassembled correctly.

Each gRPC frame has a 5-byte header: [1-byte compressed flag][4-byte big-endian length][payload].

Thread-safety: NOT thread-safe. Expected to be used by a single Netty I/O thread (per-stream handler, NOT @Sharable).

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a decoder with the default max buffer size (8 MiB).
    IncrementalGrpcFrameDecoder(int maxBufferSize)
    Creates a decoder with a custom max buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    List<byte[]>
    feed(byte[] chunk)
    Feeds a chunk of bytes into the decoder and returns a list of complete gRPC message payloads (after stripping the 5-byte header and decompressing if needed).
    boolean
    Returns true if there are buffered bytes awaiting more data to form a complete frame.

    Methods inherited from class java.lang.Object

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

    • IncrementalGrpcFrameDecoder

      public IncrementalGrpcFrameDecoder()
      Creates a decoder with the default max buffer size (8 MiB).
    • IncrementalGrpcFrameDecoder

      public IncrementalGrpcFrameDecoder(int maxBufferSize)
      Creates a decoder with a custom max buffer size.
      Parameters:
      maxBufferSize - maximum number of bytes that may be buffered; if exceeded a GrpcException is thrown from feed(byte[])
  • Method Details

    • feed

      public List<byte[]> feed(byte[] chunk)
      Feeds a chunk of bytes into the decoder and returns a list of complete gRPC message payloads (after stripping the 5-byte header and decompressing if needed).

      Any trailing partial bytes are retained internally for the next feed.

      Parameters:
      chunk - bytes to append (may be empty or null)
      Returns:
      list of complete message payloads (never null; may be empty)
      Throws:
      GrpcException - if the total buffered bytes exceed the configured cap, or if a frame header has invalid flag bits or exceeds MAX_MESSAGE_SIZE
    • hasBufferedBytes

      public boolean hasBufferedBytes()
      Returns true if there are buffered bytes awaiting more data to form a complete frame.