|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.terrier.compression.BitOutputStream
public class BitOutputStream
This class provides methods to write compressed integers to an outputstream.
The numbers are written into a byte starting from the most significant bit (i.e, left to right).
There is an internal int buffer used before writing the bytes to the underlying stream,
and the bytes are written into 32-bits integers.
Field Summary | |
---|---|
protected int |
bitOffset
The bit offset. |
protected byte[] |
buffer
Writing buffer |
protected int |
bufferPointer
poijnter for the buffer |
protected int |
bufferSize
size of the buffer it has to be 4 * k |
protected long |
byteOffset
The byte offset. |
protected int |
byteToWrite
A int to write to the stream. |
protected static int |
DEFAULT_SIZE
Default size for the buffer |
protected java.io.DataOutputStream |
dos
The private output stream used internally. |
protected static org.apache.log4j.Logger |
logger
the logger for this class |
Constructor Summary | |
---|---|
BitOutputStream()
Empty constructor |
|
BitOutputStream(java.io.OutputStream os)
Constructs an instance of the class for a given OutputSTream |
|
BitOutputStream(java.lang.String filename)
Constructs an instance of the class for a given filename Note that on a FileNotFoundException, this contructor will sleep for 2 seconds before retrying to open the file. |
Method Summary | |
---|---|
void |
append(byte[] toAppend,
int len)
Appends a byte array to the current stream. |
void |
append(byte[] toAppend,
int len,
byte newByte,
int bitswritten)
Appends a byte array to the current stream, where the last byte is not fully written Flushes the current int, the buffer and then writes the new sequence of bytes. |
void |
close()
Closes the BitOutputStream. |
void |
flush()
Deprecated. |
byte |
getBitOffset()
Returns the bit offset in the last byte. |
long |
getByteOffset()
Returns the byte offset of the stream. |
void |
padAndFlush()
Pads the current byte and writes the current int into the buffer. |
int |
writeBinary(int len,
int x)
Writes an integer in binary format to the stream. |
int |
writeDelta(int x)
Writes an integer x into the stream using delta encoding. |
int |
writeGamma(int x)
Writes an integer x into the stream using gamma encoding. |
int |
writeGolomb(int x,
int b)
Writes and integer x into the stream using golomb coding. |
int |
writeInt(int x,
int len)
Writes an integer x into the underlying OutputStream. |
int |
writeInterpolativeCode(int[] data,
int offset,
int len,
int lo,
int hi)
Writes a sequence of integers using interpolative coding. |
int |
writeMinimalBinary(int x,
int b)
Writes an integer x using minimal binary encoding, given an upper bound. |
int |
writeSkewedGolomb(int x,
int b)
Writes and integer x into the stream using skewed-golomb coding. |
int |
writeUnary(int x)
Writes an integer x using unary encoding. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final org.apache.log4j.Logger logger
protected byte[] buffer
protected int bufferPointer
protected int bufferSize
protected static final int DEFAULT_SIZE
protected java.io.DataOutputStream dos
protected long byteOffset
protected int bitOffset
protected int byteToWrite
Constructor Detail |
---|
public BitOutputStream()
public BitOutputStream(java.io.OutputStream os) throws java.io.IOException
os
- the java.io.OutputStream used for writting
java.io.IOException
- if an I/O error occurspublic BitOutputStream(java.lang.String filename) throws java.io.IOException
filename
- String with the name of the underlying file
java.io.IOException
- if an I/O error occursMethod Detail |
---|
public long getByteOffset()
getByteOffset
in interface BitOut
public byte getBitOffset()
getBitOffset
in interface BitOut
public void append(byte[] toAppend, int len) throws java.io.IOException
toAppend
- byte[] it is going to be written to the stream.len
- length in bytes of the byte buffer (number of elements of the array).
java.io.IOException
- if an I/O exception occurs.public void append(byte[] toAppend, int len, byte newByte, int bitswritten) throws java.io.IOException
toAppend
- byte[] it is going to be written to the stream.len
- length in bytes of the byte buffer (number of elements of the array).newByte
- last byte (the one not fully written)bitswritten
- number of bits written in the last byte
java.io.IOException
- if an I/O exception occurs.public void padAndFlush() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void flush()
public void close() throws java.io.IOException
close
in interface java.io.Closeable
java.io.IOException
- if an I/O error occurs when closing the underlying OutputStreampublic int writeUnary(int x) throws java.io.IOException
writeUnary
in interface BitOut
x
- the number to write
java.io.IOException
- if an I/O error occurs.public int writeGamma(int x) throws java.io.IOException
writeGamma
in interface BitOut
x
- the int number to write
java.io.IOException
- if an I/O error occurs.public int writeDelta(int x) throws java.io.IOException
writeDelta
in interface BitOut
x
- the int number to write
java.io.IOException
- if an I/O error occurs.public int writeInt(int x, int len) throws java.io.IOException
writeInt
in interface BitOut
x
- the int to writelen
- length of the int in bits
java.io.IOException
- if an I/O error occurs.public int writeSkewedGolomb(int x, int b) throws java.io.IOException
v = <b, 2b, 4b, ... , 2^i b, ...>
an integer x
is coded as unary(k+1)
where k
is the index
sum(i=0)(k) v_i < x <= sum(i=0)(k+1)
k = log(x/b + 1)
sum_i = b(2^n -1)
(geometric progression)
and the remainder with log(v_k)
bits in binary
if lower = ceil(x/b) -> lower = 2^i * b -> i = log(ceil(x/b)) + 1
the remainder x - sum_i 2^i*b - 1 = x - b(2^n - 1) - 1
is coded with floor(log(v_k))
bits
This method is not failsafe, it doesn't check if the argument or the modulus is 0 or negative.
writeSkewedGolomb
in interface BitOut
x
- the number to writeb
- the parameter for golomb coding
java.io.IOException
- if and I/O error occurspublic int writeInterpolativeCode(int[] data, int offset, int len, int lo, int hi) throws java.io.IOException
writeInterpolativeCode
in interface BitOut
data
- the vector containing the integer sequence.offset
- the offset into data
where the sequence starts.len
- the number of integers to code.lo
- a lower bound (must be smaller than or equal to the first integer in the sequence).hi
- an upper bound (must be greater than or equal to the last integer in the sequence).
java.io.IOException
- if an I/O error occurs.public int writeGolomb(int x, int b) throws java.io.IOException
writeGolomb
in interface BitOut
x
- the number to writeb
- the parameter for golomb coding
java.io.IOException
- if and I/O error occurspublic int writeMinimalBinary(int x, int b) throws java.io.IOException
writeMinimalBinary
in interface BitOut
x
- the number to writeb
- and strict bound for x
java.io.IOException
- if an I/O error occurs.public int writeBinary(int len, int x) throws java.io.IOException
writeBinary
in interface BitOut
len
- size in bits of the number.x
- the integer to write.
java.io.IOException
- if an I/O error occurs.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |