Package org.terrier.compression.bit
Class MemoryOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.terrier.compression.bit.MemoryOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
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
Fields Modifier and Type Field Description protected int
avail
Number of free bytes before having to enlargeprotected byte[]
buffer
The internal buffer.static int
DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytesprotected int
enlargeQ
Enlarge size of the arrayprotected int
pos
The current position in the buffer.
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Empty methodvoid
flush()
Empty methodbyte[]
getBuffer()
int
getPos()
int
getSize()
Get sizejava.lang.String
toString()
Writes the sequence of bytes in the byte[] into String formatvoid
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.
-
-
-
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 classjava.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 classjava.io.OutputStream
- Parameters:
b
- byte[] array containing the bytes to writeoffset
- int offset where the data to write beginslength
- 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 interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Empty method- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.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 classjava.lang.Object
-
-