org.terrier.compression
Class MemoryOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.terrier.compression.MemoryOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class MemoryOutputStream
extends java.io.OutputStream

This class extends an ordinary OutputStream to handle transparently writes in memory. This means that this class doesn't flush to disk anything, that should be done separately (@see MSBOStream). It allocates a byte[] array of original size, that can grow and be reallocated dynamically if it is needed. The class used for handling the reallocations is @see ArrayUtils.

Author:
Roi Blanco

Field Summary
protected  int avail
          Number of free bytes before having to enlarge
protected  byte[] buffer
          The internal buffer.
static int DEFAULT_BUFFER_SIZE
          The default size of the internal buffer in bytes
protected  int enlargeQ
          Enlarge size of the array
protected  int pos
          The current position in the buffer.
 
Constructor Summary
MemoryOutputStream()
          Default constructor.
MemoryOutputStream(int bufferSize)
          Creates a new instance of this class setting the buffer size.
MemoryOutputStream(int bufferSize, int enlargeSize)
          Creates a new instance of this class setting the buffer size and the growing ratio
 
Method Summary
 void close()
          Empty method
 void flush()
          Empty method
 byte[] getBuffer()
           
 int getPos()
           
 int getSize()
          Get size
 java.lang.String toString()
          Writes the sequence of bytes in the byte[] into String format
 void write(byte[] b, int offset, int length)
          Writes a sequence of bytes to the buffer.
 void write(int b)
          Writes a byte into the buffer.
 void writeByte(byte b)
          Writes a byte into the buffer.
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytes

See Also:
Constant Field Values

buffer

protected byte[] buffer
The internal buffer.


pos

protected int pos
The current position in the buffer.


avail

protected int avail
Number of free bytes before having to enlarge


enlargeQ

protected final int enlargeQ
Enlarge size of the array

Constructor Detail

MemoryOutputStream

public MemoryOutputStream()
Default constructor. Instanciates a buffer of DEFAULT_BUFFER_SIZE size


MemoryOutputStream

public MemoryOutputStream(int bufferSize)
Creates a new instance of this class setting the buffer size.

Parameters:
bufferSize - size of the buffer (in bytes)

MemoryOutputStream

public MemoryOutputStream(int bufferSize,
                          int enlargeSize)
Creates a new instance of this class setting the buffer size and the growing ratio

Parameters:
bufferSize - size of the buffer (in bytes)
enlargeSize - size the buffer is going to grow if needed (argument to ByteArrays)
Method Detail

getPos

public int getPos()
Returns:
the position in the buffer

write

public void write(int b)
           throws java.io.IOException
Writes a byte into the buffer.

Specified by:
write in class java.io.OutputStream
Parameters:
b - int containing the byte to write.
Throws:
java.io.IOException - if an I/O error occurs.

writeByte

public void writeByte(byte b)
               throws java.io.IOException
Writes a byte into the buffer.

Parameters:
b - byte to write.
Throws:
java.io.IOException - if an I/O error occurs.

write

public void write(byte[] b,
                  int offset,
                  int length)
           throws java.io.IOException
Writes a sequence of bytes to the buffer.

Overrides:
write in class java.io.OutputStream
Parameters:
b - byte[] array containing the bytes to write
offset - int offset where the data to write begins
length - int number of elements to write
Throws:
java.io.IOException - if an I/O error occurs

getSize

public int getSize()
Get size


getBuffer

public byte[] getBuffer()
Returns:
the underlying byte[] buffer

flush

public void flush()
           throws java.io.IOException
Empty method

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Empty method

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException

toString

public java.lang.String toString()
Writes the sequence of bytes in the byte[] into String format

Overrides:
toString in class java.lang.Object


Terrier 3.5. Copyright © 2004-2011 University of Glasgow