Module bus.http

Class Http2Writer

java.lang.Object
org.miaixz.bus.http.metric.http.Http2Writer
All Implemented Interfaces:
Closeable, AutoCloseable

public class Http2Writer extends Object implements Closeable
Writes HTTP/2 transport frames.
Since:
Java 17+
Author:
Kimi Liu
  • Method Details

    • connectionPreface

      public void connectionPreface() throws IOException
      Writes the HTTP/2 connection preface. This must be sent by the client at the beginning of a connection.
      Throws:
      IOException - if an I/O error occurs.
    • applyAndAckSettings

      public void applyAndAckSettings(Http2Settings peerSettings) throws IOException
      Applies peerSettings and then sends a settings ACK.
      Parameters:
      peerSettings - The settings received from the peer.
      Throws:
      IOException - if an I/O error occurs.
    • pushPromise

      public void pushPromise(int streamId, int promisedStreamId, List<Http2Header> requestHeaders) throws IOException
      HTTP/2 only. Sends push promise headers. A push promise contains all the headers associated with a server-initiated request, and a promisedStreamId that will be used for the response frames. The push promise frame is sent as part of the response to streamId. The priority of promisedStreamId is one greater than that of streamId.
      Parameters:
      streamId - The client-initiated stream ID. Must be an odd number.
      promisedStreamId - The server-initiated stream ID. Must be an even number.
      requestHeaders - Minimally includes :method, :scheme, :authority, and :path.
      Throws:
      IOException - if an I/O error occurs.
    • flush

      public void flush() throws IOException
      Flushes all buffered data on the underlying sink.
      Throws:
      IOException - if an I/O error occurs.
    • rstStream

      public void rstStream(int streamId, Http2ErrorCode errorCode) throws IOException
      Sends a RST_STREAM frame to terminate a stream.
      Parameters:
      streamId - The stream ID.
      errorCode - The error code indicating the reason for termination.
      Throws:
      IOException - if an I/O error occurs.
    • maxDataLength

      public int maxDataLength()
      Returns the maximum size of bytes that may be sent in a single call to data(boolean, int, org.miaixz.bus.core.io.buffer.Buffer, int).
      Returns:
      The maximum data length.
    • data

      public void data(boolean outFinished, int streamId, org.miaixz.bus.core.io.buffer.Buffer source, int byteCount) throws IOException
      Sends a DATA frame.
      Parameters:
      outFinished - True if this is the last frame to be sent on this stream.
      streamId - The stream ID.
      source - The buffer to draw bytes from. May be null if byteCount is 0.
      byteCount - Must be between 0 and the minimum of source.length and maxDataLength().
      Throws:
      IOException - if an I/O error occurs.
    • settings

      public void settings(Http2Settings settings) throws IOException
      Writes HTTP/2 settings to the peer.
      Parameters:
      settings - The settings to send.
      Throws:
      IOException - if an I/O error occurs.
    • ping

      public void ping(boolean ack, int payload1, int payload2) throws IOException
      Sends a connection-level ping to the peer. ack indicates this is a reply. The data in payload1 and payload2 is opaque binary, and there are no rules on the content.
      Parameters:
      ack - True if this is a reply to a ping from the peer.
      payload1 - The first payload integer.
      payload2 - The second payload integer.
      Throws:
      IOException - if an I/O error occurs.
    • goAway

      public void goAway(int lastGoodStreamId, Http2ErrorCode errorCode, byte[] debugData) throws IOException
      Informs the peer to stop creating streams. We last processed lastGoodStreamId, or zero if no streams were processed.
      Parameters:
      lastGoodStreamId - The last stream ID that was processed, or zero if no streams were processed.
      errorCode - The reason for closing the connection.
      debugData - Opaque debug data for HTTP/2 only.
      Throws:
      IOException - if an I/O error occurs.
    • windowUpdate

      public void windowUpdate(int streamId, long windowSizeIncrement) throws IOException
      Inform peer that an additional windowSizeIncrement bytes can be sent on streamId, or on the connection if streamId is zero.
      Parameters:
      streamId - The stream ID.
      windowSizeIncrement - The number of bytes to increment the window by.
      Throws:
      IOException - if an I/O error occurs.
    • frameHeader

      public void frameHeader(int streamId, int length, byte type, byte flags) throws IOException
      Writes an HTTP/2 frame header.
      Parameters:
      streamId - The stream ID.
      length - The length of the frame payload.
      type - The frame type.
      flags - The frame flags.
      Throws:
      IOException - if an I/O error occurs.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • headers

      public void headers(boolean outFinished, int streamId, List<Http2Header> headerBlock) throws IOException
      Writes a HEADERS frame, followed by any necessary CONTINUATION frames.
      Parameters:
      outFinished - True if this is the last frame to be sent on this stream.
      streamId - The stream ID.
      headerBlock - The list of headers to write.
      Throws:
      IOException - if an I/O error occurs.