messif.objects.nio
Class BufferOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by messif.objects.nio.BufferOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, BinaryOutput
Direct Known Subclasses:
ChannelOutputStream

public class BufferOutputStream
extends java.io.OutputStream
implements BinaryOutput

Output stream that implements the BinaryOutput using an internal buffer.

If multiple threads use the same instance of this class, the access to the instance must be synchronized.

See Also:
BufferInputStream

Field Summary
static int MINIMAL_BUFFER_SIZE
          Minimal buffer size in bytes
 
Constructor Summary
BufferOutputStream(java.nio.ByteBuffer buffer)
          Creates a new instance of BufferOutputStream.
BufferOutputStream(int bufferSize, boolean bufferDirect)
          Creates a new instance of BufferOutputStream.
 
Method Summary
 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.
 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.
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MINIMAL_BUFFER_SIZE

public static final int MINIMAL_BUFFER_SIZE
Minimal buffer size in bytes

See Also:
Constant Field Values
Constructor Detail

BufferOutputStream

public BufferOutputStream(int bufferSize,
                          boolean bufferDirect)
                   throws java.lang.IllegalArgumentException
Creates a new instance of BufferOutputStream. The output operates on a newly created buffer with the specified size.

Parameters:
bufferSize - the size of the internal buffer
bufferDirect - allocate the internal buffer as direct
Throws:
java.lang.IllegalArgumentException - if there specified buffer size is not valid

BufferOutputStream

public BufferOutputStream(java.nio.ByteBuffer buffer)
Creates a new instance of BufferOutputStream. The output operates on the the given buffer.

Parameters:
buffer - the internal buffer this stream
Method Detail

isDirty

public boolean isDirty()
Returns true if there are some bytes pending in the buffer.

Returns:
true if there are some bytes pending in the buffer
See Also:
flush()

bufferedSize

public int bufferedSize()
Returns the number of bytes currently in the buffer.

Returns:
the number of bytes currently in the buffer

write

public void write(int b)
           throws java.io.IOException
Writes the specified byte to this output stream.

Specified by:
write in class java.io.OutputStream
Parameters:
b - the byte to be written
Throws:
java.io.IOException - if there was an error using writeChannel

write

public void write(byte[] bytes,
                  int off,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Overrides:
write in class java.io.OutputStream
Parameters:
bytes - the data
off - the start offset in the data
len - the number of bytes to write
Throws:
java.io.IOException - if there was an error using writeChannel

flush

public void flush()
           throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in interface BinaryOutput
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if there was an error using writeChannel

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException

prepareOutput

public java.nio.ByteBuffer prepareOutput(int minBytes)
                                  throws java.io.IOException
Description copied from interface: BinaryOutput
Returns a buffer that allows to write at least 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.

Specified by:
prepareOutput in interface BinaryOutput
Parameters:
minBytes - the minimal number of bytes that must be available for writing into the buffer
Returns:
the buffer prepared for writing
Throws:
java.io.IOException - if there was an error while preparing a buffer for minBytes bytes

write

protected void write(java.nio.ByteBuffer buffer)
              throws java.io.IOException
Writes the buffered data out. This method is responsible for writing all the buffered data from the specified buffer. That is, the data from current position of the buffer up to the actual buffer limit.

Parameters:
buffer - the buffer from which to write data
Throws:
java.io.IOException - if there was an error writing the data

write

public int write(java.nio.channels.WritableByteChannel channel)
          throws java.io.IOException
Writes the buffered data to the specified channel. The write method is called on the channel.

Parameters:
channel - the writable channel that the buffered data is written to
Returns:
number of bytes actually written
Throws:
java.io.IOException - if there was an I/O error writing to the channel

write

public byte[] write()
Writes the buffered data to a byte array. Note that this will consume all the buffered data as with the other write methods.

Returns:
the buffered data

write

public int write(java.nio.channels.FileChannel channel,
                 long position)
          throws java.io.IOException
Writes the buffered data to the specified file channel. The write method is called on the channel.

Parameters:
channel - the file channel that the buffered data is written to
position - the position in the file where to write the data
Returns:
number of bytes actually written
Throws:
java.io.IOException - if there was an I/O error writing to the channel