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

public Class ChunkedOutputStream

extends OutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream, org.apache.http.io.SessionOutputBuffer

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 byte[]
private int
private boolean
closed

True if the stream is closed.

private final SessionOutputBuffer
private boolean

Constructor Summary

AccessConstructor and Description
public
ChunkedOutputStream(final SessionOutputBuffer
The session output buffer
out
,
final int
The minimum chunk size (excluding last chunk)
bufferSize
)

Wraps a session output buffer and chunk-encodes the output.
public
ChunkedOutputStream(final SessionOutputBuffer
the output buffer to wrap
out
)

Wraps a session output buffer and chunks the output.
public
ChunkedOutputStream(final int
The minimum chunk size (excluding last chunk)
bufferSize
,
final SessionOutputBuffer
The session output buffer
out
)

Wraps a session output buffer and chunk-encodes the output.

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.
protected void
flushCache()

Writes the cache out onto the underlying stream

protected 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.
protected void
Inherited from java.io.OutputStream:
nullOutputStream

Field Detail

cacheback to summary
private final byte[] cache
cachePositionback to summary
private int cachePosition
closedback to summary
private boolean closed

True if the stream is closed.

outback to summary
private final SessionOutputBuffer out
wroteLastChunkback to summary
private boolean wroteLastChunk

Constructor Detail

ChunkedOutputStreamback to summary
public ChunkedOutputStream(final SessionOutputBuffer out, final int bufferSize) throws IOException

Wraps a session output buffer and chunk-encodes the output.

Parameters
out:SessionOutputBuffer

The session output buffer

bufferSize:int

The minimum chunk size (excluding last chunk)

Annotations
@Deprecated
Exceptions
IOException:
not thrown
ChunkedOutputStreamback to summary
public ChunkedOutputStream(final SessionOutputBuffer out) throws IOException

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:SessionOutputBuffer

the output buffer to wrap

Annotations
@Deprecated
Exceptions
IOException:
not thrown
ChunkedOutputStreamback to summary
public ChunkedOutputStream(final int bufferSize, final SessionOutputBuffer out)

Wraps a session output buffer and chunk-encodes the output.

Parameters
bufferSize:int

The minimum chunk size (excluding last chunk)

out:SessionOutputBuffer

The session output buffer

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
protected void flushCache() throws IOException

Writes the cache out onto the underlying stream

flushCacheWithAppendback to summary
protected 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
protected void writeClosingChunk() throws IOException