Package org.omegazero.http.h1
Class MessageBodyDechunker
java.lang.Object
org.omegazero.http.h1.MessageBodyDechunker
Used for parsing HTTP/1 message bodies, for example with the chunked transfer encoding.
Each MessageBodyDechunker
is initialized with a single HTTPMessage
of which the body is parsed. Data received for this HTTPMessage
is then passed to
addData(byte[])
, which parses the data and, if a full chunk was received, calls the callback passed in the constructor. If the end of the body is reached, this
callback is called with a byte array of length 0.
If chunked transfer encoding is used, a buffer size must be given. This buffer is used for temporarily storing the data chunks. If a single chunk is larger than this buffer, it will be passed to the callback as multiple chunks.
This class is not thread-safe.
- Since:
- 1.2.1
- API Note:
- Ported from the native HTTP/1 implementation of omz-proxy with minor changes
-
Constructor Summary
ConstructorsConstructorDescriptionMessageBodyDechunker
(HTTPMessage msg, Consumer<byte[]> onData) Creates a newMessageBodyDechunker
with a default buffer size of 16KiB.MessageBodyDechunker
(HTTPMessage msg, Consumer<byte[]> onData, int chunkBufferSize) Creates a newMessageBodyDechunker
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addData
(byte[] data) Parses the given data.void
end()
Forces an EOF in the current input stream of thisMessageBodyDechunker
.boolean
hasEnded()
Returnstrue
if the input stream of thisMessageBodyDechunker
has ended, either due to a call toend()
or because all data was received.boolean
Returnstrue
if all expected data was received.
-
Constructor Details
-
MessageBodyDechunker
public MessageBodyDechunker(HTTPMessage msg, Consumer<byte[]> onData) throws InvalidHTTPMessageException Creates a newMessageBodyDechunker
with a default buffer size of 16KiB.- Parameters:
msg
- TheHTTPMessage
onData
- Callback for receiving the parsed data- Throws:
InvalidHTTPMessageException
- If an error occurs while parsing the messageUnsupportedOperationException
- If the Transfer-Encoding of the message is not supported- See Also:
-
MessageBodyDechunker
public MessageBodyDechunker(HTTPMessage msg, Consumer<byte[]> onData, int chunkBufferSize) throws InvalidHTTPMessageException Creates a newMessageBodyDechunker
.- Parameters:
msg
- TheHTTPMessage
onData
- Callback for receiving the parsed datachunkBufferSize
- The buffer size for storing chunk data temporarily- Throws:
InvalidHTTPMessageException
- If an error occurs while parsing the messageUnsupportedOperationException
- If the Transfer-Encoding of the message is not supported- See Also:
-
-
Method Details
-
addData
Parses the given data.- Parameters:
data
- The data- Throws:
InvalidHTTPMessageException
- If an error occurs while parsing the data, because it is malformed
-
end
public void end()Forces an EOF in the current input stream of thisMessageBodyDechunker
. This may be used if an underlying connection closed. -
hasReceivedAllData
public boolean hasReceivedAllData()Returnstrue
if all expected data was received. This is always true if the message body does not have a predetermined size.- Returns:
true
if all expected data was received
-
hasEnded
public boolean hasEnded()Returnstrue
if the input stream of thisMessageBodyDechunker
has ended, either due to a call toend()
or because all data was received.- Returns:
true
if the stream has ended
-