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.OutputStreamThis 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 intavailNumber of free bytes before having to enlargeprotected byte[]bufferThe internal buffer.static intDEFAULT_BUFFER_SIZEThe default size of the internal buffer in bytesprotected intenlargeQEnlarge size of the arrayprotected intposThe 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 voidclose()Empty methodvoidflush()Empty methodbyte[]getBuffer()intgetPos()intgetSize()Get sizejava.lang.StringtoString()Writes the sequence of bytes in the byte[] into String formatvoidwrite(byte[] b, int offset, int length)Writes a sequence of bytes to the buffer.voidwrite(int b)Writes a byte into the buffer.voidwriteByte(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.IOExceptionWrites a byte into the buffer.- Specified by:
writein 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.IOExceptionWrites 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.IOExceptionWrites a sequence of bytes to the buffer.- Overrides:
writein 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.IOExceptionEmpty method- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionEmpty method- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein 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:
toStringin classjava.lang.Object
-
-