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 newMessageBodyDechunkerwith a default buffer size of 16KiB.MessageBodyDechunker(HTTPMessage msg, Consumer<byte[]> onData, int chunkBufferSize) Creates a newMessageBodyDechunker. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddData(byte[] data) Parses the given data.voidend()Forces an EOF in the current input stream of thisMessageBodyDechunker.booleanhasEnded()Returnstrueif the input stream of thisMessageBodyDechunkerhas ended, either due to a call toend()or because all data was received.booleanReturnstrueif all expected data was received.
-
Constructor Details
-
MessageBodyDechunker
public MessageBodyDechunker(HTTPMessage msg, Consumer<byte[]> onData) throws InvalidHTTPMessageException Creates a newMessageBodyDechunkerwith a default buffer size of 16KiB.- Parameters:
msg- TheHTTPMessageonData- 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- TheHTTPMessageonData- 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()Returnstrueif all expected data was received. This is always true if the message body does not have a predetermined size.- Returns:
trueif all expected data was received
-
hasEnded
public boolean hasEnded()Returnstrueif the input stream of thisMessageBodyDechunkerhas ended, either due to a call toend()or because all data was received.- Returns:
trueif the stream has ended
-