Class Http2FrameReader

java.lang.Object
dev.relism.flash.http2.frame.Http2FrameReader

public final class Http2FrameReader extends Object
Reads length-prefixed HTTP/2 frames from one connection's BufferedByteSource. Simpler than RequestParser by construction: HTTP/2 frames declare their length up front (the 9-byte header), so nothing is ever scanned for — Http2FrameReader only ever needs to know "do I have N bytes yet", never "where does this end".

Buffer discipline

One growable byte[] per connection, reused across every frame — the same compact-before-grow discipline RequestParser's own buffer uses. A frame's declared length is checked against Http2Limits.MAX_FRAME_SIZE_LOCAL before the buffer length-check, not after an allocation already paid for it.

Usage


 FrameHeader header = reader.readFrame();
 if (header == null) { /* clean EOF between frames — connection closing *\/ }
 // ... process header.buffer()[header.payloadOffset(), +header.length()) ...
 reader.consumeFrame(); // MUST be called before the next readFrame()
 

Thread-safety

Not thread-safe — exactly one virtual thread (the connection's demux loop) ever calls this, the same invariant every other per-connection reader in this codebase assumes.
  • Constructor Details

  • Method Details

    • readFrame

      public FrameHeader readFrame() throws IOException
      Reads the next frame's header and payload, bounded by Http2Limits.FRAME_READ_TIMEOUT_MS, and returns the reused FrameHeader flyweight positioned over it — or null on a clean EOF between frames (the peer closed the connection while nothing was in flight; not an error).

      The caller MUST call consumeFrame() exactly once after processing this frame (or deciding to discard it) and before calling this method again.

      Throws:
      Http2Exception - if the declared length exceeds Http2Limits.MAX_FRAME_SIZE_LOCAL
      EOFException - if the connection closes after a frame has already started arriving
      SocketTimeoutException - if Http2Limits.FRAME_READ_TIMEOUT_MS elapses
      IOException
    • readFrame

      public FrameHeader readFrame(long timeoutMs) throws IOException
      Reads one frame using a caller-supplied upper bound for this frame's absolute deadline.
      Throws:
      IOException
    • consumeFrame

      public void consumeFrame()
      Advances past the frame last returned by readFrame(). Zero-copy, zero-allocation.
    • frameDeadlineExpired

      public boolean frameDeadlineExpired()
      Whether a partially received frame exhausted its non-renewable absolute deadline.
    • hasBufferedInput

      public boolean hasBufferedInput()
      Whether another frame may be consumed immediately without waiting for network input.