Class ByteBufferOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
OutputStream
that is backed by a ByteBuffer
assigned to
the stream.If the buffer becomes full, one or more buffers are allocated as intermediate storage. Their content is copied to the next assigned buffer(s).
While writing to this stream, remaining()
should be checked
regularly and the production of data should be suspended if possible when no
more space is left to avoid the usage of intermediate storage.
-
Constructor Summary
ConstructorDescriptionCreates a new instance with an unset overflow buffer size.ByteBufferOutputStream
(int overflowBufferSize) Creates a new instance with the given overflow buffer size. -
Method Summary
Modifier and TypeMethodDescriptionvoid
assignBuffer
(ByteBuffer buffer) Assign a new buffer to this output stream.long
The number of bytes written to this output stream.void
clear()
Clear any buffered data and prepares the buffer for reuse.void
close()
Does not have any effect.int
Returns the size of the buffers that will be allocated as overflow buffers.long
Returns the number of bytes remaining in the assigned buffer.void
setOverflowBufferSize
(int overflowBufferSize) The size of the buffers that are allocated if the assigned buffer overflows.void
write
(byte[] data, int offset, int length) void
write
(int data) void
write
(ByteBuffer data) Copies the data from the given buffer to this output stream.void
write
(ByteBuffer data, int length) Copies length bytes from the given buffer to this output stream.Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
ByteBufferOutputStream
public ByteBufferOutputStream()Creates a new instance with an unset overflow buffer size. -
ByteBufferOutputStream
Creates a new instance with the given overflow buffer size.- Parameters:
overflowBufferSize
- the overflow buffer size to use
-
-
Method Details
-
overflowBufferSize
Returns the size of the buffers that will be allocated as overflow buffers.- Returns:
- the allocation size for the overflow buffers
-
setOverflowBufferSize
The size of the buffers that are allocated if the assigned buffer overflows.If not set, buffers are allocated with one fourth of the size of the assigned buffer or 4096 if no buffer has been assigned yet.
- Parameters:
overflowBufferSize
- the size
-
clear
Clear any buffered data and prepares the buffer for reuse. -
assignBuffer
Assign a new buffer to this output stream.If the previously used buffer had become full and intermediate storage was allocated, the data from the intermediate storage is copied to the new buffer first. Then, the new buffer is used for all subsequent write operations.
- Parameters:
buffer
- the buffer to use
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
Copies the data from the given buffer to this output stream.- Parameters:
data
- the buffer
-
write
Copies length bytes from the given buffer to this output stream.- Parameters:
data
- the bufferlength
- the number of bytes to copy
-
remaining
Returns the number of bytes remaining in the assigned buffer.A negative value indicates that the assigned buffer is full and one or more overflow buffers are being used. The absolute value of the negative number is the number of bytes in the overflow buffer(s).
- Returns:
- the bytes remaining or buffered (negative value)
-
close
Does not have any effect.May be called for consistent usage of the output stream.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
- if there is still data in intermediate storage- See Also:
-
bytesWritten
The number of bytes written to this output stream.- Returns:
- the number of bytes written
-