org.terrier.compression
Class MemoryLinkedOutputStream

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

public class MemoryLinkedOutputStream
extends java.io.OutputStream

This class implements an OutputStream that writes everything in memory, and never flushes the data to disk. It uses a Linked list of byte[] arrays, probably of different sizes, to keep track of the new allocations. This class needs more memory (the pointers) than the MemoryOutputStream class, but it avoids the reallocation of arrays in memory.

Author:
Roi Blanco

Field Summary
static int DEFAULT_BUFFER_SIZE
          The default size of the internal buffer in bytes
protected  MemoryLinkedOutputStream next
          Reference to the linked structure
 
Constructor Summary
MemoryLinkedOutputStream()
          Instanciates a MemoryLinkedOutputStream with the buffer size set to DEFAULT_BUFFER_SIZE
MemoryLinkedOutputStream(int bufferSize)
          Instanciates a MemoryLinkedOutputStream specifying the buffer size.
 
Method Summary
 void beginRead()
          Sets the structure for reading
 void flush()
          Empty method
 byte[] getBuffer()
           
 MemoryLinkedOutputStream getNext()
           
 int getPos()
           
 boolean isNext()
           
 void nextBuffer()
          Moves the pointer to the next buffer (reading)
 void write(int b)
          Writes a byte into the current buffer.
 
Methods inherited from class java.io.OutputStream
close, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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

next

protected MemoryLinkedOutputStream next
Reference to the linked structure

Constructor Detail

MemoryLinkedOutputStream

public MemoryLinkedOutputStream()
Instanciates a MemoryLinkedOutputStream with the buffer size set to DEFAULT_BUFFER_SIZE


MemoryLinkedOutputStream

public MemoryLinkedOutputStream(int bufferSize)
Instanciates a MemoryLinkedOutputStream specifying the buffer size.

Parameters:
bufferSize - int size of the first buffer
Method Detail

getNext

public MemoryLinkedOutputStream getNext()
Returns:
the next buffer in the list (next of current)

getPos

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

beginRead

public void beginRead()
Sets the structure for reading


write

public void write(int b)
           throws java.io.IOException
Writes a byte into the current buffer. If it fills the buffer, it moves to the next one (doubling the capacity).

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

getBuffer

public byte[] getBuffer()
Returns:
the current byte[] buffer.

isNext

public boolean isNext()
Returns:
true if there is a buffer next to the current one.

nextBuffer

public void nextBuffer()
Moves the pointer to the next buffer (reading)


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


Terrier 3.5. Copyright © 2004-2011 University of Glasgow