public class BufferOutputStream extends java.io.OutputStream implements BinaryOutput
BinaryOutput
using
an internal buffer
.
If multiple threads use the same instance of this class, the access to the instance must be synchronized.
BufferInputStream
Modifier and Type | Field and Description |
---|---|
static int |
MINIMAL_BUFFER_SIZE
Minimal buffer size in bytes
|
Constructor and Description |
---|
BufferOutputStream(java.nio.ByteBuffer buffer)
Creates a new instance of BufferOutputStream.
|
BufferOutputStream(int bufferSize,
boolean bufferDirect)
Creates a new instance of BufferOutputStream.
|
Modifier and Type | Method and Description |
---|---|
int |
bufferedSize()
Returns the number of bytes currently in the buffer.
|
void |
close() |
void |
flush()
Flushes this output stream and forces any buffered output bytes
to be written out.
|
boolean |
isDirty()
Returns true if there are some bytes pending in the buffer.
|
java.nio.ByteBuffer |
prepareOutput(int minBytes)
Returns a buffer that allows to write at least
minBytes . |
protected void |
setBufferedSizeLimit(long size)
Limit the buffer to the specified size.
|
byte[] |
write()
Writes the buffered data to a byte array.
|
void |
write(byte[] bytes,
int off,
int len)
Writes
len bytes from the specified byte array
starting at offset off to this output stream. |
protected void |
write(java.nio.ByteBuffer buffer)
Writes the buffered data out.
|
int |
write(java.nio.channels.FileChannel channel,
long position)
Writes the buffered data to the specified file channel.
|
void |
write(int b)
Writes the specified byte to this output stream.
|
int |
write(java.nio.channels.WritableByteChannel channel)
Writes the buffered data to the specified channel.
|
public static final int MINIMAL_BUFFER_SIZE
public BufferOutputStream(int bufferSize, boolean bufferDirect) throws java.lang.IllegalArgumentException
bufferSize
- the size of the internal bufferbufferDirect
- allocate the internal buffer as direct
java.lang.IllegalArgumentException
- if there specified buffer size is not validpublic BufferOutputStream(java.nio.ByteBuffer buffer)
buffer
- the internal buffer this streampublic boolean isDirty()
flush()
public int bufferedSize()
protected void setBufferedSizeLimit(long size)
size
- the number of bytes the buffer can accommodate (including the currently stored data)public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- the byte to be writtenjava.io.IOException
- if there was an error using writeChannelpublic void write(byte[] bytes, int off, int len) throws java.io.IOException
len
bytes from the specified byte array
starting at offset off
to this output stream.write
in class java.io.OutputStream
bytes
- the dataoff
- the start offset in the datalen
- the number of bytes to writejava.io.IOException
- if there was an error using writeChannelpublic void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in interface BinaryOutput
flush
in class java.io.OutputStream
java.io.IOException
- if there was an error using writeChannelpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
public java.nio.ByteBuffer prepareOutput(int minBytes) throws java.io.IOException
BinaryOutput
minBytes
.
If the buffer with the required space cannot be provided, an
IOException
is thrown. Note that the returned
buffer can provide more than minBytes
.prepareOutput
in interface BinaryOutput
minBytes
- the minimal number of bytes that must be available for writing into the bufferjava.io.IOException
- if there was an error while preparing a buffer for minBytes
bytesprotected void write(java.nio.ByteBuffer buffer) throws java.io.IOException
buffer
- the buffer from which to write datajava.io.IOException
- if there was an error writing the datapublic byte[] write()
public int write(java.nio.channels.WritableByteChannel channel) throws java.io.IOException
write
method is called on the channel.channel
- the writable channel that the buffered data is written tojava.io.IOException
- if there was an I/O error writing to the channelpublic int write(java.nio.channels.FileChannel channel, long position) throws java.io.IOException
write
method is called on the channel.channel
- the file channel that the buffered data is written toposition
- the position in the file where to write the datajava.io.IOException
- if there was an I/O error writing to the channel