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 bytebyteToWriteA 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 voidclose()Closes the stream.bytegetBitOffset()Returns the bit offset in the last byte.longgetByteOffset()Returns the byte offset of the stream.bytegetByteToWrite()voidpad()Fills the remaining bits of the current byte with 0sintwriteInt(int x, int len)Writes an integer x into the underlying OutputStream.intwriteUnary(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.IOExceptionConstructs 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.IOExceptionConstructs 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.IOExceptionConstructs 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.IOExceptionCloses the stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein 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:
getByteOffsetin interfaceBitOut- Overrides:
getByteOffsetin 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:
getBitOffsetin interfaceBitOut- Overrides:
getBitOffsetin classBitOutputStream- Returns:
- the bit offset in the stream.
-
writeUnary
public int writeUnary(int x) throws java.io.IOExceptionWrites 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:
writeUnaryin interfaceBitOut- Overrides:
writeUnaryin 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.IOExceptionWrites 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:
writeIntin interfaceBitOut- Overrides:
writeIntin 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.IOExceptionFills 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.
-
-