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

public Class ContentLengthOutputStream

extends OutputStream
Class Inheritance
Imports
java.io.IOException, .OutputStream, org.apache.hc.core5.http.StreamClosedException, org.apache.hc.core5.http.io.SessionOutputBuffer, org.apache.hc.core5.util.Args

Output stream that cuts off after a defined number of bytes. This class is used to send content of HTTP messages where the end of the content entity is determined by the value of the Content-Length header. Entities transferred using this stream can be maximum Long#MAX_VALUE long.

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 boolean
closed

True if the stream is closed.

private final long
contentLength

The maximum number of bytes that can be written the stream.

private final OutputStream
private long
total

Total bytes written

Constructor Summary

AccessConstructor and Description
public
ContentLengthOutputStream(final SessionOutputBuffer
Session output buffer
buffer
,
final OutputStream
Output stream
outputStream
,
final long
The maximum number of bytes that can be written to the stream. Subsequent write operations will be ignored.
contentLength
)

Default constructor.

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
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.
public void
write(final byte[]
the data.
b
,
final int
the start offset in the data.
off
,
final int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
public void
write(final byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.
public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
Inherited from java.io.OutputStream:
nullOutputStream

Field Detail

bufferback to summary
private final SessionOutputBuffer buffer
closedback to summary
private boolean closed

True if the stream is closed.

contentLengthback to summary
private final long contentLength

The maximum number of bytes that can be written the stream. Subsequent write operations will be ignored.

outputStreamback to summary
private final OutputStream outputStream
totalback to summary
private long total

Total bytes written

Constructor Detail

ContentLengthOutputStreamback to summary
public ContentLengthOutputStream(final SessionOutputBuffer buffer, final OutputStream outputStream, final long contentLength)

Default constructor.

Parameters
buffer:SessionOutputBuffer

Session output buffer

outputStream:OutputStream

Output stream

contentLength:long

The maximum number of bytes that can be written to the stream. Subsequent write operations will be ignored.

Since
4.0

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:
If an I/O problem occurs.
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

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

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
b:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
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.