Class OutputBitStream
- java.lang.Object
-
- it.unimi.dsi.io.OutputBitStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
DebugOutputBitStream
public class OutputBitStream extends Object implements Flushable, Closeable
Bit-level output stream.This class wraps any
OutputStreamso that you can treat it as bit stream. Constructors and methods closely resemble those ofOutputStream. Data can be added to such a stream in several ways: writing an integer or long in fixed-width, unary, γ, δ, ζ and Golomb coding, or providing a vector of bytes.This class can also wrap a byte array; this is much more lightweight than wrapping a
FastByteArrayOutputStreamwrapping the array, but overflowing the array will cause anIOException.Note that when writing using a vector of bytes bits are written in the natural way: the first bit is bit 7 of the first byte, the eighth bit is bit 0 of the first byte, the ninth bit is bit 7 of the second byte and so on. When writing integers using some coding, instead, the lower bits are considered for coding (in the fixed-width case, the given number of bits, otherwise the lower bits starting from the most significant one).
The bit stream format
The bit streams written by this class are big endian. That is, the first bit of the stream is bit 7 of the first byte, the eightth bit is bit 0 of the first byte, the ninth bit is bit 7 of the second byte and so on.
Blocks of bits (such as coded integers) are written starting from the most significant bit. In other words, if you take the first bytes of a stream and print them in binary you will see exactly the sequence of bits you have written. In particular, if you write 32-bit integers you will get a stream which is identical to the one produced by a
DataOutput.Additional features:
- This class provides an internal buffer. By setting a buffer of length 0 at creation time, you can actually bypass the buffering system: Note, however, that several classes providing buffering have synchronised methods, so using a wrapper instead of the internal buffer is likely to lead to a performance drop.
- To work around the schizophrenic relationship between streams and random
access files in
java.io, this class provides aflush()method that byte-aligns the streams, flushes to the underlying byte stream all data and resets the internal state. At this point, you can safely reposition the underlying stream and write again afterwards. For instance, this is safe and will perform as expected:FileOutputStream fos = new FileOutputStream(...); OutputBitStream obs = new OutputBitStream(fos); ... write operations on obs ... obs.flush(); fos.getChannel().position(...); ... other write operations on obs ...
As a commodity, an instance of this class will try to cast the underlying byte stream to a
RepositionableStreamand to fetch by reflection theFileChannelunderlying the given output stream, in this order. If either reference can be successfully fetched, you can use directly theposition()method with argumentposwith the same semantics of aflush(), followed by a call toposition(pos / 8)(where the latter method belongs either to the underlying stream or to its underlying file channel). The specified position must be byte aligned, as there is no clean way of reading a fraction of a byte with the current APIs. However, since the reflective checks are quite heavy they can be disabled using a suitable constructor.
This class is not synchronised. If multiple threads access an instance of this class concurrently, they must be synchronised externally.
- Since:
- 0.1
- Author:
- Sebastiano Vigna
- See Also:
OutputStream,InputBitStream
-
-
Field Summary
Fields Modifier and Type Field Description protected intavailCurrent number of bytes available in the byte buffer.protected byte[]bufferThe stream buffer.static intDEFAULT_BUFFER_SIZEThe default size of the byte buffer in bytes (16Ki).static int[]DELTAprotected FileChannelfileChannelThe cached file channel underlyingos.protected intfreeCurrent number of free bits in the bit buffer (the bits in the buffer are stored high).static int[]GAMMAstatic intMAX_PRECOMPUTEDprotected OutputStreamosThe underlyingOutputStream.protected intposCurrent position in the byte buffer.protected longpositionCurrent position of the underlying output stream.protected RepositionableStreamrepositionableStreamoscast to a positionable stream.static int[]SHIFTED_GAMMAprotected booleanwrappingTrue if we are wrapping an array.static int[]ZETA_3
-
Constructor Summary
Constructors Modifier Constructor Description protectedOutputBitStream()This (non-public) constructor exists just to provide fake initialisation for classes such asDebugOutputBitStream.OutputBitStream(byte[] a)Creates a new output bit stream wrapping a given byte array.OutputBitStream(File file)Creates a new output bit stream writing to a file.OutputBitStream(FileOutputStream os)Creates a new output bit stream wrapping a given file output stream using a buffer of sizeDEFAULT_BUFFER_SIZE.OutputBitStream(FileOutputStream os, int bufSize)Creates a new output bit stream wrapping a given file output stream with a specified buffer size.OutputBitStream(File file, int bufSize)Creates a new output bit stream writing to file.OutputBitStream(OutputStream os)Creates a new output bit stream wrapping a given output stream using a buffer of sizeDEFAULT_BUFFER_SIZE.OutputBitStream(OutputStream os, boolean testForPosition)Creates a new output bit stream wrapping a given output stream using a buffer of sizeDEFAULT_BUFFER_SIZE.OutputBitStream(OutputStream os, int bufSize)Creates a new output bit stream wrapping a given output stream with a specified buffer size.OutputBitStream(OutputStream os, int bufSize, boolean testForPosition)Creates a new output bit stream wrapping a given output stream with a specified buffer size.OutputBitStream(String name)Creates a new output bit stream writing to a file.OutputBitStream(String name, int bufSize)Creates a new output bit stream writing to file.
-
Method Summary
Modifier and Type Method Description intalign()Aligns the stream.voidclose()Closes the bit stream.voidcopyFrom(InputBitStream ibs, long length)Copies a given number of bits from a given input bit stream into this output bit stream.voidflush()Flushes the bit stream.voidposition(long position)Sets this stream bit position, if it is based on aRepositionableStreamor on aFileChannel.longwrite(byte[] bits, long len)Writes a sequence of bits.longwrite(byte[] bits, long offset, long len)Writes a sequence of bits, starting from a given offset.intwrite(BooleanIterator i)Writes a sequence of bits emitted by a boolean iterator.intwriteBit(boolean bit)Writes a bit.intwriteBit(int bit)Writes a bit.protected longwriteByteOffset(byte[] bits, int offset, long len)Writes a sequence of bits, starting from a given byte offset.intwriteDelta(int x)Writes a natural number in δ coding.longwriteDeltas(int[] a, int count)Writes a given amount of natural numbers in δ coding.intwriteGamma(int x)Writes a natural number in γ coding.longwriteGammas(int[] a, int count)Writes a given amount of natural numbers in γ coding.intwriteGolomb(int x, int b)Writes a natural number in Golomb coding.intwriteGolomb(int x, int b, int log2b)Writes a natural number in Golomb coding.intwriteInt(int x, int len)Writes a fixed number of bits from an integer.intwriteLong(long x, int len)Writes a fixed number of bits from a long.intwriteLongDelta(long x)Writes a long natural number in δ coding.intwriteLongGamma(long x)Writes a long natural number in γ coding.longwriteLongGolomb(long x, long b)Writes a long natural number in Golomb coding.longwriteLongGolomb(long x, long b, int log2b)Writes a long natural number in Golomb coding.intwriteLongMinimalBinary(long x, long b)Writes a long natural number in a limited range using a minimal binary coding.intwriteLongMinimalBinary(long x, long b, int log2b)Writes a long natural number in a limited range using a minimal binary coding.intwriteLongNibble(long x)Writes a long natural number in variable-length nibble coding.intwriteLongShiftedGamma(long x)Writes a long natural number in shifted γ coding.longwriteLongSkewedGolomb(long x, long b)Writes a long natural number in skewed Golomb coding.longwriteLongUnary(long x)Writes a long natural number in unary coding.intwriteLongZeta(long x, int k)Writes a long natural number in ζ coding.intwriteMinimalBinary(int x, int b)Writes a natural number in a limited range using a minimal binary coding.intwriteMinimalBinary(int x, int b, int log2b)Writes a natural number in a limited range using a minimal binary coding.intwriteNibble(int x)Writes a natural number in variable-length nibble coding.intwriteShiftedGamma(int x)Writes a natural number in shifted γ coding.longwriteShiftedGammas(int[] a, int count)Writes a given amount of natural numbers in shifted γ coding.intwriteSkewedGolomb(int x, int b)Writes a natural number in skewed Golomb coding.intwriteUnary(int x)Writes a natural number in unary coding.intwriteZeta(int x, int k)Writes a natural number in ζ coding.longwrittenBits()Returns the number of bits written to this bit stream.voidwrittenBits(long writtenBits)Sets the number of bits written to this bit stream.
-
-
-
Field Detail
-
MAX_PRECOMPUTED
public static final int MAX_PRECOMPUTED
- See Also:
- Constant Field Values
-
GAMMA
public static final int[] GAMMA
-
DELTA
public static final int[] DELTA
-
ZETA_3
public static final int[] ZETA_3
-
SHIFTED_GAMMA
public static final int[] SHIFTED_GAMMA
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default size of the byte buffer in bytes (16Ki).- See Also:
- Constant Field Values
-
os
protected final OutputStream os
The underlyingOutputStream.
-
buffer
protected byte[] buffer
The stream buffer.
-
free
protected int free
Current number of free bits in the bit buffer (the bits in the buffer are stored high).
-
pos
protected int pos
Current position in the byte buffer.
-
position
protected long position
Current position of the underlying output stream.
-
avail
protected int avail
Current number of bytes available in the byte buffer.
-
fileChannel
protected final FileChannel fileChannel
The cached file channel underlyingos.
-
repositionableStream
protected final RepositionableStream repositionableStream
oscast to a positionable stream.
-
wrapping
protected final boolean wrapping
True if we are wrapping an array.
-
-
Constructor Detail
-
OutputBitStream
protected OutputBitStream()
This (non-public) constructor exists just to provide fake initialisation for classes such asDebugOutputBitStream.
-
OutputBitStream
public OutputBitStream(OutputStream os)
Creates a new output bit stream wrapping a given output stream using a buffer of sizeDEFAULT_BUFFER_SIZE.This constructor performs the reflective tests that are necessary to support
position(long).- Parameters:
os- the output stream to wrap.
-
OutputBitStream
public OutputBitStream(OutputStream os, boolean testForPosition)
Creates a new output bit stream wrapping a given output stream using a buffer of sizeDEFAULT_BUFFER_SIZE.- Parameters:
os- the output stream to wrap.testForPosition- if false, the reflective test that is necessary to supportposition(long)in caseosdoes not supportRepositionableStreamwill not be performed.
-
OutputBitStream
public OutputBitStream(OutputStream os, int bufSize)
Creates a new output bit stream wrapping a given output stream with a specified buffer size.This constructor performs the reflective tests that are necessary to support
position(long).- Parameters:
os- the output stream to wrap.bufSize- the size in byte of the buffer; it may be 0, denoting no buffering.
-
OutputBitStream
public OutputBitStream(OutputStream os, int bufSize, boolean testForPosition)
Creates a new output bit stream wrapping a given output stream with a specified buffer size.- Parameters:
os- the output stream to wrap.bufSize- the size in byte of the buffer; it may be 0, denoting no buffering.testForPosition- if false, the reflective test that is necessary to supportposition(long)in caseosdoes not supportRepositionableStreamwill not be performed.
-
OutputBitStream
public OutputBitStream(FileOutputStream os)
Creates a new output bit stream wrapping a given file output stream using a buffer of sizeDEFAULT_BUFFER_SIZE.This constructor invokes directly
FileOutputStream.getChannel()to supportposition(long).- Parameters:
os- the output stream to wrap.
-
OutputBitStream
public OutputBitStream(FileOutputStream os, int bufSize)
Creates a new output bit stream wrapping a given file output stream with a specified buffer size.This constructor invokes directly
FileOutputStream.getChannel()to supportposition(long).- Parameters:
os- the output stream to wrap.bufSize- the size in byte of the buffer; it may be 0, denoting no buffering.
-
OutputBitStream
public OutputBitStream(byte[] a)
Creates a new output bit stream wrapping a given byte array.- Parameters:
a- the byte array to wrap.
-
OutputBitStream
public OutputBitStream(String name, int bufSize) throws FileNotFoundException
Creates a new output bit stream writing to file.- Parameters:
name- the name of the file.bufSize- the size in byte of the buffer; it may be 0, denoting no buffering.- Throws:
FileNotFoundException
-
OutputBitStream
public OutputBitStream(String name) throws FileNotFoundException
Creates a new output bit stream writing to a file.- Parameters:
name- the name of the file.- Throws:
FileNotFoundException
-
OutputBitStream
public OutputBitStream(File file, int bufSize) throws FileNotFoundException
Creates a new output bit stream writing to file.- Parameters:
file- the file.bufSize- the size in byte of the buffer; it may be 0, denoting no buffering.- Throws:
FileNotFoundException
-
OutputBitStream
public OutputBitStream(File file) throws FileNotFoundException
Creates a new output bit stream writing to a file.- Parameters:
file- the file.- Throws:
FileNotFoundException
-
-
Method Detail
-
flush
public void flush() throws IOExceptionFlushes the bit stream.This method will align the stream, write the bit buffer, empty the byte buffer and delegate to the
OutputStream.flush()method of the underlying output stream.This method is provided so that users of this class can easily wrap repositionable streams (for instance, file-based streams, which can be repositioned using the underlying
FileChannel).It is guaranteed that after calling this method the underlying stream can be repositioned, and that the next write to the underlying output stream will start with the content of the first write method called afterwards.
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
public void close() throws IOExceptionCloses the bit stream. All resources associated with the stream are released.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
writtenBits
public long writtenBits()
Returns the number of bits written to this bit stream.- Returns:
- the number of bits written so far.
-
writtenBits
public void writtenBits(long writtenBits)
Sets the number of bits written to this bit stream.This method is provided so that, for instance, the user can reset via
writtenBits(0)the written-bits count after aflush().- Parameters:
writtenBits- the new value for the number of bits written so far.
-
align
public int align() throws IOExceptionAligns the stream. After a call to this method, the stream is byte aligned. Zeroes are used to pad it if necessary.- Returns:
- the number of padding bits.
- Throws:
IOException
-
position
public void position(long position) throws IOExceptionSets this stream bit position, if it is based on aRepositionableStreamor on aFileChannel.Given an underlying stream that implements
RepositionableStreamor that can provide aFileChannelvia thegetChannel()method, a call to this method has the same semantics of aflush(), followed by a call toposition(position / 8)on the byte stream. Currently there is no clean, working way of supporting out-of-byte-boundary positioning.- Parameters:
position- the new position expressed as a bit offset; it must be byte-aligned.- Throws:
IllegalArgumentException- when trying to position outside of byte boundaries.UnsupportedOperationException- if the underlying byte stream does not implementRepositionableStreamand if the channel it returns is not aFileChannel.IOException- See Also:
FileChannel.position(long)
-
write
public long write(byte[] bits, long len) throws IOExceptionWrites a sequence of bits. Bits will be written in the natural way: the first bit is bit 7 of the first byte, the eightth bit is bit 0 of the first byte, the ninth bit is bit 7 of the second byte and so on.- Parameters:
bits- a vector containing the bits to be written.len- a bit length.- Returns:
- the number of bits written (
len). - Throws:
IOException
-
write
public long write(byte[] bits, long offset, long len) throws IOExceptionWrites a sequence of bits, starting from a given offset. Bits will be written in the natural way: the first bit is bit 7 of the first byte, the eightth bit is bit 0 of the first byte, the ninth bit is bit 7 of the second byte and so on.- Parameters:
bits- a vector containing the bits to be written.offset- a bit offset from which to start to write.len- a bit length.- Returns:
- the number of bits written (
len). - Throws:
IOException
-
writeByteOffset
protected long writeByteOffset(byte[] bits, int offset, long len) throws IOExceptionWrites a sequence of bits, starting from a given byte offset. Bits will be written in the natural way: the first bit is bit 7 of the first byte, the eightth bit is bit 0 of the first byte, the ninth bit is bit 7 of the second byte and so on.This method is used to support methods such as
write(byte[], long, long).- Parameters:
bits- a vector containing the bits to be written.offset- an offset, expressed in bytes.len- a bit length.- Returns:
- the number of bits written (
len). - Throws:
IOException
-
writeBit
public int writeBit(boolean bit) throws IOExceptionWrites a bit.- Parameters:
bit- a bit.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeBit
public int writeBit(int bit) throws IOExceptionWrites a bit.- Parameters:
bit- a bit.- Returns:
- the number of bits written.
- Throws:
IOException
-
write
public int write(BooleanIterator i) throws IOException
Writes a sequence of bits emitted by a boolean iterator.If the iterator throws an exception, it is catched, and the return value is given by the number of bits written increased by one and with the sign changed.
- Parameters:
i- a boolean iterator.- Returns:
- if
idid not throw a runtime exception, the number of bits written; otherwise, the number of bits written, plus one, with the sign changed. - Throws:
IOException
-
writeInt
public int writeInt(int x, int len) throws IOExceptionWrites a fixed number of bits from an integer.- Parameters:
x- an integer.len- a bit length; this many lower bits of the first argument will be written (the most significant bit first).- Returns:
- the number of bits written (
len). - Throws:
IOException
-
writeLong
public int writeLong(long x, int len) throws IOExceptionWrites a fixed number of bits from a long.- Parameters:
x- a long.len- a bit length; this many lower bits of the first argument will be written (the most significant bit first).- Returns:
- the number of bits written (
len). - Throws:
IOException
-
writeUnary
public int writeUnary(int x) throws IOExceptionWrites a natural number in unary coding.The unary coding of a natural number n is given by 0n1.
- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException
-
writeLongUnary
public long writeLongUnary(long x) throws IOExceptionWrites a long natural number in unary coding.- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeUnary(int)
-
writeGamma
public int writeGamma(int x) throws IOExceptionWrites a natural number in γ coding.The γ coding of a positive number of k bits is obtained writing k-1 in unary, followed by the lower k-1 bits of the number. The coding of a natural number is obtained by adding one and coding.
- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException
-
writeGammas
public long writeGammas(int[] a, int count) throws IOExceptionWrites a given amount of natural numbers in γ coding.- Parameters:
a- an array at leastcountnatural numbers.count- the number of elements ofato be written.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeGamma(int)
-
writeLongGamma
public int writeLongGamma(long x) throws IOExceptionWrites a long natural number in γ coding.- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeGamma(int)
-
writeShiftedGamma
public int writeShiftedGamma(int x) throws IOExceptionWrites a natural number in shifted γ coding. The shifted γ coding of 0 is 1. The coding of a positive number of k bits is obtained writing k in unary, followed by the lower k-1 bits of the number (equivalently, by writing k zeroes followed by the number).- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException
-
writeLongShiftedGamma
public int writeLongShiftedGamma(long x) throws IOExceptionWrites a long natural number in shifted γ coding.- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeShiftedGamma(int)
-
writeShiftedGammas
public long writeShiftedGammas(int[] a, int count) throws IOExceptionWrites a given amount of natural numbers in shifted γ coding.- Parameters:
a- an array at leastcountnatural numbers.count- the number of elements ofato be written.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeShiftedGamma(int)
-
writeDelta
public int writeDelta(int x) throws IOExceptionWrites a natural number in δ coding. The δ coding of a positive number of k bits is obtained writing k-1 in γ coding, followed by the lower k-1 bits of the number. The coding of a natural number is obtained by adding one and coding.- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException
-
writeLongDelta
public int writeLongDelta(long x) throws IOExceptionWrites a long natural number in δ coding.- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeDelta(int)
-
writeDeltas
public long writeDeltas(int[] a, int count) throws IOExceptionWrites a given amount of natural numbers in δ coding.- Parameters:
a- an array at leastcountnatural numbers.count- the number of elements ofato be written.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeDelta(int)
-
writeMinimalBinary
public int writeMinimalBinary(int x, int b) throws IOExceptionWrites a natural number in a limited range using a minimal binary coding.A minimal binary code is an optimal code for the uniform distribution. This method uses an optimal code in which shorter words are assigned to smaller integers.
- Parameters:
x- a natural number.b- a strict upper bound forx.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a nonpositive base.IOException
-
writeMinimalBinary
public int writeMinimalBinary(int x, int b, int log2b) throws IOExceptionWrites a natural number in a limited range using a minimal binary coding. This method is faster thanwriteMinimalBinary(int,int)because it does not have to computelog2b.- Parameters:
x- a natural number.b- a strict upper bound forx.log2b- the floor of the base-2 logarithm of the bound.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a nonpositive base.IOException- See Also:
writeMinimalBinary(int, int)
-
writeLongMinimalBinary
public int writeLongMinimalBinary(long x, long b) throws IOExceptionWrites a long natural number in a limited range using a minimal binary coding.- Parameters:
x- a natural number.b- a strict upper bound forx.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a nonpositive base.IOException- See Also:
writeMinimalBinary(int, int)
-
writeLongMinimalBinary
public int writeLongMinimalBinary(long x, long b, int log2b) throws IOExceptionWrites a long natural number in a limited range using a minimal binary coding. This method is faster thanwriteLongMinimalBinary(long,long)because it does not have to computelog2b.- Parameters:
x- a long natural number.b- a strict upper bound forx.log2b- the floor of the base-2 logarithm of the bound.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a nonpositive base.IOException- See Also:
writeMinimalBinary(int, int)
-
writeGolomb
public int writeGolomb(int x, int b) throws IOExceptionWrites a natural number in Golomb coding.Golomb coding with modulo b writes a natural number x as the quotient of the division of x and b in unary, followed by the remainder in minimal binary code.
This method implements also the case in which
bis 0: in this case, the argumentxmay only be zero, and nothing will be written.- Parameters:
x- a natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a negative modulus.IOException
-
writeGolomb
public int writeGolomb(int x, int b, int log2b) throws IOExceptionWrites a natural number in Golomb coding. This method is faster thanwriteGolomb(int,int)because it does not have to computelog2b.- Parameters:
x- a natural number.b- the modulus for the coding.log2b- the floor of the base-2 logarithm of the coding modulus (it is irrelevant whenbis zero).- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a negative modulus.IOException- See Also:
writeGolomb(int, int)
-
writeLongGolomb
public long writeLongGolomb(long x, long b) throws IOExceptionWrites a long natural number in Golomb coding.- Parameters:
x- a long natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a negative modulus.IOException- See Also:
writeGolomb(int, int)
-
writeLongGolomb
public long writeLongGolomb(long x, long b, int log2b) throws IOExceptionWrites a long natural number in Golomb coding. This method is faster thanwriteLongGolomb(long,long)because it does not have to computelog2b.- Parameters:
x- a long natural number.b- the modulus for the coding.log2b- the floor of the base-2 logarithm of the coding modulus (it is irrelevant whenbis zero).- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a negative modulus.IOException- See Also:
writeGolomb(int, int)
-
writeSkewedGolomb
public int writeSkewedGolomb(int x, int b) throws IOExceptionWrites a natural number in skewed Golomb coding.This method implements also the case in which
bis 0: in this case, the argumentxmay only be zero, and nothing will be written.- Parameters:
x- a natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a negative modulus.IOException
-
writeLongSkewedGolomb
public long writeLongSkewedGolomb(long x, long b) throws IOExceptionWrites a long natural number in skewed Golomb coding.This method implements also the case in which
bis 0: in this case, the argumentxmay only be zero, and nothing will be written.- Parameters:
x- a long natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a negative modulus.IOException- See Also:
writeSkewedGolomb(int, int)
-
writeZeta
public int writeZeta(int x, int k) throws IOExceptionWrites a natural number in ζ coding.ζ coding (with modulo k) records positive numbers in the intervals [1,2k-1],[2k,2k+1-1],…,[2hk,2(h+1)k-1] by coding h in unary, followed by a minimal binary coding of the offset in the interval. The coding of a natural number is obtained by adding one and coding.
ζ codes were defined by Paolo Boldi and Sebastiano Vigna in “Codes for the World−Wide Web”, Internet Math., 2(4):405-427, 2005. The paper contains also a detailed analysis.
- Parameters:
x- a natural number.k- the shrinking factor.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a nonpositive shrinking factor.IOException
-
writeLongZeta
public int writeLongZeta(long x, int k) throws IOExceptionWrites a long natural number in ζ coding.- Parameters:
x- a long natural number.k- the shrinking factor.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number or use a nonpositive shrinking factor.IOException- See Also:
writeZeta(int, int)
-
writeNibble
public int writeNibble(int x) throws IOExceptionWrites a natural number in variable-length nibble coding.Variable-length nibble coding records a natural number by padding its binary representation to the left using zeroes, until its length is a multiple of three. Then, the resulting string is broken in blocks of 3 bits, and each block is prefixed with a bit, which is zero for all blocks except for the last one.
- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException
-
writeLongNibble
public int writeLongNibble(long x) throws IOExceptionWrites a long natural number in variable-length nibble coding.- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IllegalArgumentException- if you try to write a negative number.IOException- See Also:
writeNibble(int)
-
copyFrom
public void copyFrom(InputBitStream ibs, long length) throws IOException
Copies a given number of bits from a given input bit stream into this output bit stream.- Parameters:
ibs- an input bit stream.length- the number of bits to copy.- Throws:
EOFException- if there are not enough bits to copy.IOException
-
-