Class DebugOutputBitStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class DebugOutputBitStream extends OutputBitStream
This class can be used to wrap an output bit stream. The semantics of the resulting write operations is unchanged, but each operation will be logged.
To simplify the output, some operations have a simplified representation. In particular:
|flush();->position();[- creation;
]close();{x}- explicit bits;
{x:b}- minimal binary coding of x with bound b;
{M:x}- write x with coding M; the latter can be U (unary), g (γ), z (ζ), d (δ), G (Golomb), GS (skewed Golomb); when appropriate, x is followed by an extra integer (modulus, etc.).
- Since:
- 0.7.1
- Author:
- Paolo Boldi, Sebastiano Vigna
-
Field Summary
Fields inherited from class it.unimi.dsi.io.OutputBitStream
avail, buffer, DEFAULT_BUFFER_SIZE, DELTA, fileChannel, free, GAMMA, MAX_PRECOMPUTED, os, pos, position, repositionableStream, SHIFTED_GAMMA, wrapping, ZETA_3 -
Constructor Summary
Constructors Constructor Description DebugOutputBitStream(OutputBitStream obs)Creates a new debug output bit stream wrapping a given output bit stream and logging on standard error.DebugOutputBitStream(OutputBitStream obs, PrintStream pw)Creates a new debug output bit stream wrapping a given output bit stream and logging on a given writer. -
Method Summary
Modifier and Type Method Description intalign()Aligns the stream.voidclose()Closes the 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.intwriteBit(boolean bit)Writes a bit.intwriteBit(int bit)Writes a bit.intwriteDelta(int x)Writes a natural number in δ coding.intwriteGamma(int x)Writes a natural number 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.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.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.Methods inherited from class it.unimi.dsi.io.OutputBitStream
copyFrom, write, write, writeByteOffset, writeDeltas, writeGammas, writeLongShiftedGamma, writeShiftedGamma, writeShiftedGammas
-
Constructor Details
-
DebugOutputBitStream
Creates a new debug output bit stream wrapping a given output bit stream and logging on a given writer.- Parameters:
obs- the output bit stream to wrap.pw- a print stream that will receive the logging data.
-
DebugOutputBitStream
Creates a new debug output bit stream wrapping a given output bit stream and logging on standard error.- Parameters:
obs- the output bit stream to wrap.
-
-
Method Details
-
flush
Description copied from class:OutputBitStreamFlushes 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- Overrides:
flushin classOutputBitStream- Throws:
IOException
-
close
Description copied from class:OutputBitStreamCloses the bit stream. All resources associated with the stream are released.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputBitStream- Throws:
IOException
-
writtenBits
public long writtenBits()Description copied from class:OutputBitStreamReturns the number of bits written to this bit stream.- Overrides:
writtenBitsin classOutputBitStream- Returns:
- the number of bits written so far.
-
writtenBits
public void writtenBits(long writtenBits)Description copied from class:OutputBitStreamSets 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 aOutputBitStream.flush().- Overrides:
writtenBitsin classOutputBitStream- Parameters:
writtenBits- the new value for the number of bits written so far.
-
align
Description copied from class:OutputBitStreamAligns the stream. After a call to this method, the stream is byte aligned. Zeroes are used to pad it if necessary.- Overrides:
alignin classOutputBitStream- Returns:
- the number of padding bits.
- Throws:
IOException
-
position
Description copied from class:OutputBitStreamSets 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 aOutputBitStream.flush(), 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.- Overrides:
positionin classOutputBitStream- Parameters:
position- the new position expressed as a bit offset; it must be byte-aligned.- Throws:
IOException- See Also:
FileChannel.position(long)
-
write
Description copied from class:OutputBitStreamWrites 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.- Overrides:
writein classOutputBitStream- Parameters:
bits- a vector containing the bits to be written.len- a bit length.- Returns:
- the number of bits written (
len). - Throws:
IOException
-
writeBit
Description copied from class:OutputBitStreamWrites a bit.- Overrides:
writeBitin classOutputBitStream- Parameters:
bit- a bit.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeBit
Description copied from class:OutputBitStreamWrites a bit.- Overrides:
writeBitin classOutputBitStream- Parameters:
bit- a bit.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeInt
Description copied from class:OutputBitStreamWrites a fixed number of bits from an integer.- Overrides:
writeIntin classOutputBitStream- 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
Description copied from class:OutputBitStreamWrites a fixed number of bits from a long.- Overrides:
writeLongin classOutputBitStream- 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
Description copied from class:OutputBitStreamWrites a natural number in unary coding.The unary coding of a natural number n is given by 0n1.
- Overrides:
writeUnaryin classOutputBitStream- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeLongUnary
Description copied from class:OutputBitStreamWrites a long natural number in unary coding.- Overrides:
writeLongUnaryin classOutputBitStream- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeUnary(int)
-
writeGamma
Description copied from class:OutputBitStreamWrites 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.
- Overrides:
writeGammain classOutputBitStream- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeLongGamma
Description copied from class:OutputBitStreamWrites a long natural number in γ coding.- Overrides:
writeLongGammain classOutputBitStream- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeGamma(int)
-
writeDelta
Description copied from class:OutputBitStreamWrites 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.- Overrides:
writeDeltain classOutputBitStream- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeLongDelta
Description copied from class:OutputBitStreamWrites a long natural number in δ coding.- Overrides:
writeLongDeltain classOutputBitStream- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeDelta(int)
-
writeMinimalBinary
Description copied from class:OutputBitStreamWrites 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.
- Overrides:
writeMinimalBinaryin classOutputBitStream- Parameters:
x- a natural number.b- a strict upper bound forx.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeMinimalBinary
Description copied from class:OutputBitStreamWrites a natural number in a limited range using a minimal binary coding. This method is faster thanOutputBitStream.writeMinimalBinary(int,int)because it does not have to computelog2b.- Overrides:
writeMinimalBinaryin classOutputBitStream- 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:
IOException- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
writeLongMinimalBinary
Description copied from class:OutputBitStreamWrites a long natural number in a limited range using a minimal binary coding.- Overrides:
writeLongMinimalBinaryin classOutputBitStream- Parameters:
x- a natural number.b- a strict upper bound forx.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
writeLongMinimalBinary
Description copied from class:OutputBitStreamWrites a long natural number in a limited range using a minimal binary coding. This method is faster thanOutputBitStream.writeLongMinimalBinary(long,long)because it does not have to computelog2b.- Overrides:
writeLongMinimalBinaryin classOutputBitStream- 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:
IOException- See Also:
OutputBitStream.writeMinimalBinary(int, int)
-
writeGolomb
Description copied from class:OutputBitStreamWrites 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.- Overrides:
writeGolombin classOutputBitStream- Parameters:
x- a natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeGolomb
Description copied from class:OutputBitStreamWrites a natural number in Golomb coding. This method is faster thanOutputBitStream.writeGolomb(int,int)because it does not have to computelog2b.- Overrides:
writeGolombin classOutputBitStream- 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:
IOException- See Also:
OutputBitStream.writeGolomb(int, int)
-
writeLongGolomb
Description copied from class:OutputBitStreamWrites a long natural number in Golomb coding.- Overrides:
writeLongGolombin classOutputBitStream- Parameters:
x- a long natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeGolomb(int, int)
-
writeLongGolomb
Description copied from class:OutputBitStreamWrites a long natural number in Golomb coding. This method is faster thanOutputBitStream.writeLongGolomb(long,long)because it does not have to computelog2b.- Overrides:
writeLongGolombin classOutputBitStream- 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:
IOException- See Also:
OutputBitStream.writeGolomb(int, int)
-
writeSkewedGolomb
Description copied from class:OutputBitStreamWrites 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.- Overrides:
writeSkewedGolombin classOutputBitStream- Parameters:
x- a natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeLongSkewedGolomb
Description copied from class:OutputBitStreamWrites 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.- Overrides:
writeLongSkewedGolombin classOutputBitStream- Parameters:
x- a long natural number.b- the modulus for the coding.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeSkewedGolomb(int, int)
-
writeZeta
Description copied from class:OutputBitStreamWrites 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.
- Overrides:
writeZetain classOutputBitStream- Parameters:
x- a natural number.k- the shrinking factor.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeLongZeta
Description copied from class:OutputBitStreamWrites a long natural number in ζ coding.- Overrides:
writeLongZetain classOutputBitStream- Parameters:
x- a long natural number.k- the shrinking factor.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeZeta(int, int)
-
writeNibble
Description copied from class:OutputBitStreamWrites 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.
- Overrides:
writeNibblein classOutputBitStream- Parameters:
x- a natural number.- Returns:
- the number of bits written.
- Throws:
IOException
-
writeLongNibble
Description copied from class:OutputBitStreamWrites a long natural number in variable-length nibble coding.- Overrides:
writeLongNibblein classOutputBitStream- Parameters:
x- a long natural number.- Returns:
- the number of bits written.
- Throws:
IOException- See Also:
OutputBitStream.writeNibble(int)
-