Class Http3FrameWriter

java.lang.Object
io.github.mahmoudimus.http3.Http3FrameWriter

public final class Http3FrameWriter extends Object
Writes HTTP/3 frames to one QUIC stream. Each call builds its frame in memory and hands it to the stream in one write. A QUIC stream has a single writer, so there is no locking; if several threads share one stream, they must serialize their calls. Nothing is flushed until flush().

Arguments that would produce a frame the protocol forbids (an HTTP/2-only setting, say) are rejected with IllegalArgumentException before anything is written. The static encode(Http3Frame) methods give the bytes of a frame without a stream.

  • Constructor Details

    • Http3FrameWriter

      public Http3FrameWriter(OutputStream out)
      A writer for one stream.
      Parameters:
      out - the stream's bytes; give it a buffered stream and flush it as needed
  • Method Details

    • writeStreamType

      public void writeStreamType(long streamType) throws IOException
      Writes the stream type that must start a unidirectional stream; see Http3StreamType.
      Parameters:
      streamType - the stream type
      Throws:
      IOException - if writing fails
    • writeFrame

      public void writeFrame(Http3Frame frame) throws IOException
      Writes one frame exactly as given.
      Parameters:
      frame - the frame
      Throws:
      IOException - if writing fails
    • writeData

      public void writeData(byte[] data, int offset, int length) throws IOException
      One DATA frame.
      Parameters:
      data - the content
      offset - where it starts in data
      length - how many bytes
      Throws:
      IOException - if writing fails
    • writeData

      public void writeData(byte[] data) throws IOException
      One DATA frame with all of data.
      Parameters:
      data - the content
      Throws:
      IOException - if writing fails
    • writeHeaders

      public void writeHeaders(byte[] fieldSection) throws IOException
      A HEADERS frame carrying an encoded field section from QpackEncoder.
      Parameters:
      fieldSection - the encoded field section
      Throws:
      IOException - if writing fails
    • writeSettings

      public void writeSettings(Http3Settings settings) throws IOException
      A SETTINGS frame.
      Parameters:
      settings - the settings
      Throws:
      IOException - if writing fails
    • writePushPromise

      public void writePushPromise(long pushId, byte[] fieldSection) throws IOException
      A PUSH_PROMISE frame.
      Parameters:
      pushId - the push it promises
      fieldSection - the encoded field section of the promised request
      Throws:
      IOException - if writing fails
    • writeCancelPush

      public void writeCancelPush(long pushId) throws IOException
      A CANCEL_PUSH frame.
      Parameters:
      pushId - the push to cancel
      Throws:
      IOException - if writing fails
    • writeGoAway

      public void writeGoAway(long id) throws IOException
      A GOAWAY frame.
      Parameters:
      id - the stream or push id
      Throws:
      IOException - if writing fails
    • writeMaxPushId

      public void writeMaxPushId(long pushId) throws IOException
      A MAX_PUSH_ID frame.
      Parameters:
      pushId - the largest push id the server may use
      Throws:
      IOException - if writing fails
    • flush

      public void flush() throws IOException
      Flushes the stream.
      Throws:
      IOException - if flushing fails
    • encode

      public static byte[] encode(Http3Frame frame)
      The bytes of one frame.
      Parameters:
      frame - the frame
      Returns:
      its type, length and payload
    • encode

      public static void encode(Http3Frame frame, ByteBuffer buf)
      Puts the bytes of one frame at the buffer's position.
      Parameters:
      frame - the frame
      buf - where to put it
      Throws:
      BufferOverflowException - if it does not fit; nothing is written then