Package io.github.mahmoudimus.http2
Class FrameWriter
java.lang.Object
io.github.mahmoudimus.http2.FrameWriter
Writes HTTP/2 frames to a blocking stream. Each call builds its frames in memory and hands them
to the stream in one
write, so frames from different threads never interleave as long
as callers serialize their calls (this class does no locking; hold a lock, or give one thread
the job of writing). Nothing is flushed until flush().
Arguments that would produce a frame the protocol forbids, including a payload larger than
the peer's SETTINGS_MAX_FRAME_SIZE, are rejected with
IllegalArgumentException before anything is written. writeHeaders(int, byte[], boolean) and
writePushPromise(int, int, byte[]) split long field blocks into CONTINUATION frames; DATA is never split,
because how much may be sent is a flow-control decision for the caller.
-
Constructor Summary
ConstructorsConstructorDescriptionFrameWriter(OutputStream out) Creates a writer for the supplied output stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidflush()Flushes buffered output to the underlying stream.intReturns the current frame payload limit.voidsetMaxFrameSize(int maxFrameSize) The largest payload the peer accepts: its SETTINGS_MAX_FRAME_SIZE (16384 until it says otherwise).voidWrites the client connection preface; a client then sends its SETTINGS.voidwriteData(int streamId, byte[] data, int offset, int length, boolean endStream) One DATA frame, unpadded.voidwriteFrame(Frame frame) Writes one frame exactly as given.voidwriteGoAway(int lastStreamId, ErrorCode error, byte[] debugData) Writes a connection shutdown frame.voidwriteHeaders(int streamId, byte[] fieldBlock, boolean endStream) A complete field block: one HEADERS frame, followed by CONTINUATION frames if it is too long for one.voidwritePing(boolean ack, long opaqueData) Writes a connection PING.voidwritePushPromise(int streamId, int promisedStreamId, byte[] fieldBlock) A complete PUSH_PROMISE field block, with CONTINUATION frames as needed.voidwriteRstStream(int streamId, ErrorCode error) Terminates one stream.voidwriteSettings(Http2Settings settings) Writes the settings that differ from the protocol defaults.voidwriteSettings(Map<Integer, Long> values) Writes the supplied setting identifiers and values.voidAcknowledges the peer SETTINGS.voidwriteWindowUpdate(int streamId, int increment) Returns flow-control credit to the peer.
-
Constructor Details
-
FrameWriter
Creates a writer for the supplied output stream.- Parameters:
out- the non-null stream to write
-
-
Method Details
-
setMaxFrameSize
public void setMaxFrameSize(int maxFrameSize) The largest payload the peer accepts: its SETTINGS_MAX_FRAME_SIZE (16384 until it says otherwise).- Parameters:
maxFrameSize- the maximum payload size from 2^14 to 2^24-1 octets
-
maxFrameSize
public int maxFrameSize()Returns the current frame payload limit.- Returns:
- the maximum payload size in octets
-
writeClientPreface
Writes the client connection preface; a client then sends its SETTINGS.- Throws:
IOException- if writing to the underlying stream fails
-
writeFrame
Writes one frame exactly as given. A HEADERS or PUSH_PROMISE without END_HEADERS must be followed by its CONTINUATION frames, with nothing in between.- Parameters:
frame- the frame to serialize- Throws:
IOException- if writing to the underlying stream fails
-
writeData
public void writeData(int streamId, byte[] data, int offset, int length, boolean endStream) throws IOException One DATA frame, unpadded.- Parameters:
streamId- the nonzero stream identifierdata- the content byte arrayoffset- the first byte to sendlength- the number of bytes to sendendStream- whether this frame ends the stream- Throws:
IOException- if writing to the underlying stream fails
-
writeHeaders
A complete field block: one HEADERS frame, followed by CONTINUATION frames if it is too long for one.- Parameters:
streamId- the nonzero stream identifierfieldBlock- the complete HPACK-encoded field blockendStream- whether the HEADERS frame ends the stream- Throws:
IOException- if writing to the underlying stream fails
-
writePushPromise
public void writePushPromise(int streamId, int promisedStreamId, byte[] fieldBlock) throws IOException A complete PUSH_PROMISE field block, with CONTINUATION frames as needed.- Parameters:
streamId- the nonzero stream identifierpromisedStreamId- the nonzero identifier of the promised streamfieldBlock- the complete HPACK-encoded field block- Throws:
IOException- if writing to the underlying stream fails
-
writeSettings
Writes the settings that differ from the protocol defaults.- Parameters:
settings- the local settings to advertise- Throws:
IOException- if writing to the underlying stream fails
-
writeSettings
Writes the supplied setting identifiers and values.- Parameters:
values- the settings to advertise- Throws:
IOException- if writing to the underlying stream fails
-
writeSettingsAck
Acknowledges the peer SETTINGS.- Throws:
IOException- if writing to the underlying stream fails
-
writePing
Writes a connection PING.- Parameters:
ack- whether to acknowledge a peer PINGopaqueData- the eight opaque octets as a big-endian long- Throws:
IOException- if writing to the underlying stream fails
-
writeGoAway
Writes a connection shutdown frame.- Parameters:
lastStreamId- the last peer-initiated stream that might have been processederror- the shutdown error codedebugData- diagnostic bytes, or null for none- Throws:
IOException- if writing to the underlying stream fails
-
writeRstStream
Terminates one stream.- Parameters:
streamId- the nonzero stream identifiererror- the termination error code- Throws:
IOException- if writing to the underlying stream fails
-
writeWindowUpdate
Returns flow-control credit to the peer.- Parameters:
streamId- the stream identifier, or 0 for the connectionincrement- the positive credit in octets- Throws:
IOException- if writing to the underlying stream fails
-
flush
Flushes buffered output to the underlying stream.- Throws:
IOException- if writing to the underlying stream fails
-