Package org.apache.http.impl.io
Class ChunkedOutputStream
java.lang.Object
java.io.OutputStream
org.apache.http.impl.io.ChunkedOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class ChunkedOutputStream extends OutputStream
Implements chunked transfer coding.
See RFC 2616,
section 3.6.1.
Writes are buffered to an internal buffer (2048 default size).
- Since:
- 4.0
- Author:
- Mohammad Rezaei (Goldman, Sachs & Co.), Oleg Kalnichevski
-
Constructor Summary
Constructors Constructor Description ChunkedOutputStream(SessionOutputBuffer out)Wraps a session output buffer and chunks the output.ChunkedOutputStream(SessionOutputBuffer out, int bufferSize)Wraps a session output buffer and chunks the output. -
Method Summary
Modifier and Type Method Description voidclose()Finishes writing to the underlying stream, but does NOT close the underlying stream.voidfinish()Must be called to ensure the internal cache is flushed and the closing chunk is written.voidflush()Flushes the content buffer and the underlying stream.protected voidflushCache()Writes the cache out onto the underlying streamprotected voidflushCacheWithAppend(byte[] bufferToAppend, int off, int len)Writes the cache and bufferToAppend to the underlying stream as one large chunkvoidwrite(byte[] b)Writes the array.voidwrite(byte[] src, int off, int len)Writescountbytes from the byte arraybufferstarting at positionoffsetto this stream.voidwrite(int b)Writes a single byte to this stream.protected voidwriteClosingChunk()
-
Constructor Details
-
ChunkedOutputStream
Wraps a session output buffer and chunks the output.- Parameters:
out- the session output buffer to wrapbufferSize- minimum chunk size (excluding last chunk)- Throws:
IOException
-
ChunkedOutputStream
Wraps a session output buffer and chunks the output. The default buffer size of 2048 was chosen because the chunk overhead is less than 0.5%- Parameters:
out- the output buffer to wrap- Throws:
IOException
-
-
Method Details
-
flushCache
Writes the cache out onto the underlying stream- Throws:
IOException
-
flushCacheWithAppend
Writes the cache and bufferToAppend to the underlying stream as one large chunk- Parameters:
bufferToAppend-off-len-- Throws:
IOException
-
writeClosingChunk
- Throws:
IOException
-
finish
Must be called to ensure the internal cache is flushed and the closing chunk is written.- Throws:
IOException
-
write
Description copied from class:OutputStreamWrites a single byte to this stream. Only the least significant byte of the integeroneByteis written to the stream.- Specified by:
writein classOutputStream- Parameters:
b- the byte to be written.- Throws:
IOException- if an error occurs while writing to this stream.
-
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.- Overrides:
writein classOutputStream- Parameters:
b-- Throws:
IOException
-
write
Description copied from class:OutputStreamWritescountbytes from the byte arraybufferstarting at positionoffsetto this stream.- Overrides:
writein classOutputStream- Parameters:
src- the buffer to be written.off- the start position inbufferfrom where to get bytes.len- the number of bytes frombufferto write to this stream.- Throws:
IOException- if an error occurs while writing to this stream.
-
flush
Flushes the content buffer and the underlying stream.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
Finishes writing to the underlying stream, but does NOT close the underlying stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-