public class HttpChunkedInput extends java.lang.Object implements ChunkedInput<HttpContent>
ChunkedInput that fetches data chunk by chunk for use with HTTP chunked transfers.
Each chunk from the input data will be wrapped within a HttpContent. At the end of the input data,
LastHttpContent will be written.
Ensure that your HTTP response header contains Transfer-Encoding: chunked.
public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.headers().set(TRANSFER_ENCODING, CHUNKED);
ctx.write(response);
HttpContentChunkedInput httpChunkWriter = new HttpChunkedInput(
new ChunkedFile("/tmp/myfile.txt"));
ChannelFuture sendFileFuture = ctx.write(httpChunkWriter);
}
| Constructor and Description |
|---|
HttpChunkedInput(ChunkedInput<ByteBuf> input)
Creates a new instance using the specified input.
|
HttpChunkedInput(ChunkedInput<ByteBuf> input,
LastHttpContent lastHttpContent)
Creates a new instance using the specified input.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Releases the resources associated with the input.
|
boolean |
isEndOfInput()
Return
true if and only if there is no data left in the stream
and the stream has reached at its end. |
HttpContent |
readChunk(ChannelHandlerContext ctx)
Fetches a chunked data from the stream.
|
public HttpChunkedInput(ChunkedInput<ByteBuf> input)
input - ChunkedInput containing data to writepublic HttpChunkedInput(ChunkedInput<ByteBuf> input, LastHttpContent lastHttpContent)
lastHttpContent will be written as the terminating
chunk.input - ChunkedInput containing data to writelastHttpContent - LastHttpContent that will be written as the terminating chunk. Use this for
training headers.public boolean isEndOfInput()
throws java.lang.Exception
ChunkedInputtrue if and only if there is no data left in the stream
and the stream has reached at its end.isEndOfInput in interface ChunkedInput<HttpContent>java.lang.Exceptionpublic void close()
throws java.lang.Exception
ChunkedInputclose in interface ChunkedInput<HttpContent>java.lang.Exceptionpublic HttpContent readChunk(ChannelHandlerContext ctx) throws java.lang.Exception
ChunkedInputChunkedInput.isEndOfInput()
call must return true.readChunk in interface ChunkedInput<HttpContent>null if there is no data left in the stream.
Please note that null does not necessarily mean that the
stream has reached at its end. In a slow stream, the next chunk
might be unavailable just momentarily.java.lang.ExceptionCopyright © 2008–2018 The Netty Project. All rights reserved.