public class BitOutputStream extends Object implements BitOut
Modifier and Type | Field and Description |
---|---|
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 DataOutputStream |
dos
The private output stream used internally.
|
protected static org.apache.log4j.Logger |
logger
the logger for this class
|
Constructor and Description |
---|
BitOutputStream()
Empty constructor
|
BitOutputStream(OutputStream os)
Constructs an instance of the class for a given OutputSTream
|
BitOutputStream(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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
protected static final org.apache.log4j.Logger logger
protected byte[] buffer
protected int bufferPointer
protected int bufferSize
protected static final int DEFAULT_SIZE
protected DataOutputStream dos
protected long byteOffset
protected int bitOffset
protected int byteToWrite
public BitOutputStream()
public BitOutputStream(OutputStream os) throws IOException
os
- the java.io.OutputStream used for writtingIOException
- if an I/O error occurspublic BitOutputStream(String filename) throws IOException
filename
- String with the name of the underlying fileIOException
- if an I/O error occurspublic long getByteOffset()
getByteOffset
in interface BitOut
public byte getBitOffset()
getBitOffset
in interface BitOut
public void append(byte[] toAppend, int len) throws 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).IOException
- if an I/O exception occurs.public void append(byte[] toAppend, int len, byte newByte, int bitswritten) throws 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 byteIOException
- if an I/O exception occurs.public void padAndFlush() throws IOException
IOException
- if an I/O error occurs.public void flush()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if an I/O error occurs when closing the underlying OutputStreampublic int writeUnary(int x) throws IOException
writeUnary
in interface BitOut
x
- the number to writeIOException
- if an I/O error occurs.public int writeGamma(int x) throws IOException
writeGamma
in interface BitOut
x
- the int number to writeIOException
- if an I/O error occurs.public int writeDelta(int x) throws IOException
writeDelta
in interface BitOut
x
- the int number to writeIOException
- if an I/O error occurs.public int writeInt(int x, int len) throws IOException
writeInt
in interface BitOut
x
- the int to writelen
- length of the int in bitsIOException
- if an I/O error occurs.public int writeSkewedGolomb(int x, int b) throws 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 codingIOException
- if and I/O error occurspublic int writeInterpolativeCode(int[] data, int offset, int len, int lo, int hi) throws 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).IOException
- if an I/O error occurs.public int writeGolomb(int x, int b) throws IOException
writeGolomb
in interface BitOut
x
- the number to writeb
- the parameter for golomb codingIOException
- if and I/O error occurspublic int writeMinimalBinary(int x, int b) throws IOException
writeMinimalBinary
in interface BitOut
x
- the number to writeb
- and strict bound for x
IOException
- if an I/O error occurs.public int writeBinary(int len, int x) throws IOException
writeBinary
in interface BitOut
len
- size in bits of the number.x
- the integer to write.IOException
- if an I/O error occurs.Terrier 4.0. Copyright © 2004-2014 University of Glasgow