Package org.apache.hc.core5.http.impl.io
Class ChunkedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.hc.core5.http.impl.io.ChunkedOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class ChunkedOutputStream extends java.io.OutputStream
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
Fields Modifier and Type Field Description private SessionOutputBuffer
buffer
private byte[]
cache
private int
cachePosition
private boolean
closed
private CharArrayBuffer
lineBuffer
private java.io.OutputStream
outputStream
private Supplier<java.util.List<? extends Header>>
trailerSupplier
private boolean
wroteLastChunk
-
Constructor Summary
Constructors Constructor Description ChunkedOutputStream(SessionOutputBuffer buffer, java.io.OutputStream outputStream, byte[] chunkCache, Supplier<java.util.List<? extends Header>> trailerSupplier)
Default constructor.ChunkedOutputStream(SessionOutputBuffer buffer, java.io.OutputStream outputStream, int chunkSizeHint)
Constructor with no trailers.ChunkedOutputStream(SessionOutputBuffer buffer, java.io.OutputStream outputStream, int chunkSizeHint, Supplier<java.util.List<? extends Header>> trailerSupplier)
Constructor taking an integer chunk size hint.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Finishes writing to the underlying stream, but does NOT close the underlying stream.void
finish()
Must be called to ensure the internal cache is flushed and the closing chunk is written.void
flush()
Flushes the content buffer and the underlying stream.private void
flushCache()
Writes the cache out onto the underlying streamprivate void
flushCacheWithAppend(byte[] bufferToAppend, int off, int len)
Writes the cache and bufferToAppend to the underlying stream as one large chunkvoid
write(byte[] b)
Writes the array.void
write(byte[] src, int off, int len)
Writes the array.void
write(int b)
private void
writeClosingChunk()
private void
writeTrailers()
-
-
-
Field Detail
-
buffer
private final SessionOutputBuffer buffer
-
outputStream
private final java.io.OutputStream outputStream
-
cache
private final byte[] cache
-
cachePosition
private int cachePosition
-
wroteLastChunk
private boolean wroteLastChunk
-
closed
private boolean closed
-
lineBuffer
private final CharArrayBuffer lineBuffer
-
-
Constructor Detail
-
ChunkedOutputStream
public ChunkedOutputStream(SessionOutputBuffer buffer, java.io.OutputStream outputStream, byte[] chunkCache, Supplier<java.util.List<? extends Header>> trailerSupplier)
Default constructor.- Parameters:
buffer
- Session output bufferoutputStream
- Output streamchunkCache
- Buffer used to aggregate smaller writes into chunks.trailerSupplier
- Trailer supplier. May benull
- Since:
- 5.1
-
ChunkedOutputStream
public ChunkedOutputStream(SessionOutputBuffer buffer, java.io.OutputStream outputStream, int chunkSizeHint, Supplier<java.util.List<? extends Header>> trailerSupplier)
Constructor taking an integer chunk size hint.- Parameters:
buffer
- Session output bufferoutputStream
- Output streamchunkSizeHint
- minimal chunk size hinttrailerSupplier
- Trailer supplier. May benull
- Since:
- 5.0
-
ChunkedOutputStream
public ChunkedOutputStream(SessionOutputBuffer buffer, java.io.OutputStream outputStream, int chunkSizeHint)
Constructor with no trailers.- Parameters:
buffer
- Session output bufferoutputStream
- Output streamchunkSizeHint
- minimal chunk size hint
-
-
Method Detail
-
flushCache
private void flushCache() throws java.io.IOException
Writes the cache out onto the underlying stream- Throws:
java.io.IOException
-
flushCacheWithAppend
private void flushCacheWithAppend(byte[] bufferToAppend, int off, int len) throws java.io.IOException
Writes the cache and bufferToAppend to the underlying stream as one large chunk- Throws:
java.io.IOException
-
writeClosingChunk
private void writeClosingChunk() throws java.io.IOException
- Throws:
java.io.IOException
-
writeTrailers
private void writeTrailers() throws java.io.IOException
- Throws:
java.io.IOException
-
finish
public void finish() throws java.io.IOException
Must be called to ensure the internal cache is flushed and the closing chunk is written.- Throws:
java.io.IOException
- in case of an I/O error
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException
Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] src, int off, int len) throws java.io.IOException
Writes the array. If the array does not fit within the buffer, it is not split, but rather written out as one large chunk.- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
Flushes the content buffer and the underlying stream.- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Finishes writing to the underlying stream, but does NOT close the underlying stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
-