Package org.terrier.compression.bit
Class MemoryLinkedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.terrier.compression.bit.MemoryLinkedOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
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
Fields Modifier and Type Field Description static int
DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytesprotected MemoryLinkedOutputStream
next
Reference to the linked structure
-
Constructor Summary
Constructors Constructor Description MemoryLinkedOutputStream()
Instanciates a MemoryLinkedOutputStream with the buffer size set to DEFAULT_BUFFER_SIZEMemoryLinkedOutputStream(int bufferSize)
Instanciates a MemoryLinkedOutputStream specifying the buffer size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginRead()
Sets the structure for readingvoid
flush()
Empty methodbyte[]
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.
-
-
-
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 classjava.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 interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
-