Package org.terrier.compression.bit
Class BitByteOutputStream
- java.lang.Object
-
- org.terrier.compression.bit.BitOutputStream
-
- org.terrier.compression.bit.BitByteOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,BitOut
- Direct Known Subclasses:
MemorySBOS
public class BitByteOutputStream extends BitOutputStream
An implementation of BitOutputStream that does no buffering. The numbers are written into a byte starting from the most significant bit (i.e, left to right). This class does not use any buffering, it relies on the underlying OutputStream to decide when to flush to disk. It is used for indexing, when keeping many compressed streams in memory (MemoryOutputStream) and a two-level buffering is unnecessary (and slower).- Author:
- Roi Blanco
-
-
Field Summary
Fields Modifier and Type Field Description protected byte
byteToWrite
A byte to write to the stream.-
Fields inherited from class org.terrier.compression.bit.BitOutputStream
bitOffset, buffer, bufferPointer, bufferSize, byteOffset, DEFAULT_SIZE, dos, logger
-
-
Constructor Summary
Constructors Constructor Description BitByteOutputStream()
Empty constructor, used for subclassingBitByteOutputStream(java.io.File file)
Constructs an instance of the class for a given fileBitByteOutputStream(java.io.OutputStream is)
Constructs an instance of the class for a given streamBitByteOutputStream(java.lang.String filename)
Constructs an instance of the class for a given filename
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the stream.byte
getBitOffset()
Returns the bit offset in the last byte.long
getByteOffset()
Returns the byte offset of the stream.byte
getByteToWrite()
void
pad()
Fills the remaining bits of the current byte with 0sint
writeInt(int x, int len)
Writes an integer x into the underlying OutputStream.int
writeUnary(int x)
Writes an integer x using unary encoding.-
Methods inherited from class org.terrier.compression.bit.BitOutputStream
append, append, flush, padAndFlush, writeBinary, writeDelta, writeGamma, writeGolomb, writeInterpolativeCode, writeMinimalBinary, writeSkewedGolomb
-
-
-
-
Constructor Detail
-
BitByteOutputStream
public BitByteOutputStream(java.io.OutputStream is) throws java.io.IOException
Constructs an instance of the class for a given stream- Parameters:
is
- java.io.OutputStream the underlying input stream- Throws:
java.io.IOException
- if an I/O error occurs
-
BitByteOutputStream
public BitByteOutputStream(java.lang.String filename) throws java.io.IOException
Constructs an instance of the class for a given filename- Parameters:
filename
- java.lang.String the name of the underlying file- Throws:
java.io.IOException
- if an I/O error occurs
-
BitByteOutputStream
public BitByteOutputStream(java.io.File file) throws java.io.IOException
Constructs an instance of the class for a given file- Parameters:
file
- java.io.File the underlying file- Throws:
java.io.IOException
- if an I/O error occurs
-
BitByteOutputStream
public BitByteOutputStream()
Empty constructor, used for subclassing
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes the stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classBitOutputStream
- Throws:
java.io.IOException
- if an I/O error occurs
-
getByteOffset
public long getByteOffset()
Returns the byte offset of the stream. It corresponds to the position of the byte in which the next bit will be written.- Specified by:
getByteOffset
in interfaceBitOut
- Overrides:
getByteOffset
in classBitOutputStream
- Returns:
- the byte offset in the stream.
-
getBitOffset
public byte getBitOffset()
Returns the bit offset in the last byte. It corresponds to the position in which the next bit will be written.- Specified by:
getBitOffset
in interfaceBitOut
- Overrides:
getBitOffset
in classBitOutputStream
- Returns:
- the bit offset in the stream.
-
writeUnary
public int writeUnary(int x) throws java.io.IOException
Writes an integer x using unary encoding. The encoding is a sequence of x -1 zeros and 1 one: 1, 01, 001, 0001, etc .. This method is not failsafe, it doesn't check if the argument is 0 or negative.- Specified by:
writeUnary
in interfaceBitOut
- Overrides:
writeUnary
in classBitOutputStream
- Parameters:
x
- the number to write- Returns:
- the number of bits written
- Throws:
java.io.IOException
- if an I/O error occurs.
-
writeInt
public int writeInt(int x, int len) throws java.io.IOException
Writes an integer x into the underlying OutputStream. First, it checks if it fits into the current byte we are using for writing, and then it writes as many bytes as necessary- Specified by:
writeInt
in interfaceBitOut
- Overrides:
writeInt
in classBitOutputStream
- Parameters:
x
- the int to writelen
- length of the int in bits- Returns:
- the number of bits written
- Throws:
java.io.IOException
- if an I/O error occurs.
-
pad
public void pad() throws java.io.IOException
Fills the remaining bits of the current byte with 0s- Throws:
java.io.IOException
-
getByteToWrite
public byte getByteToWrite()
- Returns:
- the actual byte it is being written.
-
-