Top Description Fields Constructors Methods
org.apache.hc.core5.http.impl.io

public Class ChunkedOutputStream

extends OutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream, java.util.List, org.apache.hc.core5.function.Supplier, org.apache.hc.core5.http.FormattedHeader, .Header, .StreamClosedException, org.apache.hc.core5.http.io.SessionOutputBuffer, org.apache.hc.core5.http.message.BasicLineFormatter, org.apache.hc.core5.util.Args, .CharArrayBuffer

Implements chunked transfer coding. The content is sent in small chunks. Entities transferred using this output stream can be of unlimited length. Writes are buffered to an internal buffer (2048 default size).

Note that this class NEVER closes the underlying stream, even when close() gets called. Instead, the stream will be marked as closed and no further output will be permitted.

Since
4.0

Field Summary

Modifier and TypeField and Description
private final SessionOutputBuffer
private final byte[]
private int
private boolean
private final CharArrayBuffer
private final OutputStream
private final Supplier<List<? extends Header>>
private boolean

Constructor Summary

AccessConstructor and Description
public
ChunkedOutputStream(final SessionOutputBuffer
Session output buffer
buffer
,
final OutputStream
Output stream
outputStream
,
final byte[]
Buffer used to aggregate smaller writes into chunks.
chunkCache
,
final Supplier<List<? extends Header>>
Trailer supplier. May be null
trailerSupplier
)

Default constructor.

public
ChunkedOutputStream(final SessionOutputBuffer
Session output buffer
buffer
,
final OutputStream
Output stream
outputStream
,
final int
minimal chunk size hint
chunkSizeHint
,
final Supplier<List<? extends Header>>
Trailer supplier. May be null
trailerSupplier
)

Constructor taking an integer chunk size hint.

public
ChunkedOutputStream(final SessionOutputBuffer
Session output buffer
buffer
,
final OutputStream
Output stream
outputStream
,
final int
minimal chunk size hint
chunkSizeHint
)

Constructor with no trailers.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Finishes writing to the underlying stream, but does NOT close the underlying stream.
public void
finish()

Must be called to ensure the internal cache is flushed and the closing chunk is written.

public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes the content buffer and the underlying stream.
private void
flushCache()

Writes the cache out onto the underlying stream

private void
flushCacheWithAppend(final byte[] bufferToAppend, final int off, final int len)

Writes the cache and bufferToAppend to the underlying stream as one large chunk

public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
public void
write(final byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes the array.
public void
write(final byte[]
the data.
src
,
final int
the start offset in the data.
off
,
final int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes the array.
private void
private void
Inherited from java.io.OutputStream:
nullOutputStream

Field Detail

bufferback to summary
private final SessionOutputBuffer buffer
cacheback to summary
private final byte[] cache
cachePositionback to summary
private int cachePosition
closedback to summary
private boolean closed
lineBufferback to summary
private final CharArrayBuffer lineBuffer
outputStreamback to summary
private final OutputStream outputStream
trailerSupplierback to summary
private final Supplier<List<? extends Header>> trailerSupplier
wroteLastChunkback to summary
private boolean wroteLastChunk

Constructor Detail

ChunkedOutputStreamback to summary
public ChunkedOutputStream(final SessionOutputBuffer buffer, final OutputStream outputStream, final byte[] chunkCache, final Supplier<List<? extends Header>> trailerSupplier)

Default constructor.

Parameters
buffer:SessionOutputBuffer

Session output buffer

outputStream:OutputStream

Output stream

chunkCache:byte[]

Buffer used to aggregate smaller writes into chunks.

trailerSupplier:Supplier<List<? extends Header>>

Trailer supplier. May be null

Since
5.1
ChunkedOutputStreamback to summary
public ChunkedOutputStream(final SessionOutputBuffer buffer, final OutputStream outputStream, final int chunkSizeHint, final Supplier<List<? extends Header>> trailerSupplier)

Constructor taking an integer chunk size hint.

Parameters
buffer:SessionOutputBuffer

Session output buffer

outputStream:OutputStream

Output stream

chunkSizeHint:int

minimal chunk size hint

trailerSupplier:Supplier<List<? extends Header>>

Trailer supplier. May be null

Since
5.0
ChunkedOutputStreamback to summary
public ChunkedOutputStream(final SessionOutputBuffer buffer, final OutputStream outputStream, final int chunkSizeHint)

Constructor with no trailers.

Parameters
buffer:SessionOutputBuffer

Session output buffer

outputStream:OutputStream

Output stream

chunkSizeHint:int

minimal chunk size hint

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Finishes writing to the underlying stream, but does NOT close the underlying stream.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.OutputStream.close.

if an I/O error occurs.

finishback to summary
public void finish() throws IOException

Must be called to ensure the internal cache is flushed and the closing chunk is written.

Exceptions
IOException:
in case of an I/O error
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes the content buffer and the underlying stream.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.OutputStream.flush.

if an I/O error occurs.

flushCacheback to summary
private void flushCache() throws IOException

Writes the cache out onto the underlying stream

flushCacheWithAppendback to summary
private void flushCacheWithAppend(final byte[] bufferToAppend, final int off, final int len) throws IOException

Writes the cache and bufferToAppend to the underlying stream as one large chunk

writeback to summary
public void write(final int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(final byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.

Parameters
b:byte[]

Doc from java.io.OutputStream.write.

the data.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.OutputStream.write.

if an I/O error occurs.

writeback to summary
public void write(final byte[] src, final int off, final int len) throws IOException

Overrides java.io.OutputStream.write.

Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.

Parameters
src:byte[]

Doc from java.io.OutputStream.write.

the data.

off:int

Doc from java.io.OutputStream.write.

the start offset in the data.

len:int

Doc from java.io.OutputStream.write.

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.OutputStream.write.

if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

writeClosingChunkback to summary
private void writeClosingChunk() throws IOException
writeTrailersback to summary
private void writeTrailers() throws IOException