Implemented BitOutput#skipBits(long) and ByteOutput#skipBytes(int)

This commit is contained in:
Collin Smith 2020-08-10 18:34:54 -07:00
parent 984a0864ea
commit 8941ceee6e
2 changed files with 31 additions and 0 deletions

View File

@ -63,6 +63,31 @@ public class BitOutput {
return this;
}
public BitOutput skipBits(long bits) {
if (bits < 0) throw new IllegalArgumentException("bits(" + bits + ") < " + 0);
if (bits == 0) return this;
// aligns bit stream for multi-byte skipping
assert bitsCached < Byte.SIZE : "bitsCached(" + bitsCached + ") > " + (Byte.SIZE - 1);
final int bitsUncached = Byte.SIZE - bitsCached;
if (bits >= bitsUncached) {
bits -= bitsUncached;
flush();
}
// skips multiple bytes
final long startingBitsWritten = bitsWritten;
final long bytes = bits / Byte.SIZE;
assert bytes <= Integer.MAX_VALUE : "bytes(" + bytes + ") > Integer.MAX_VALUE";
if (bytes > 0) align().skipBytes((int) bytes);
// skips overflow bits
final long overflowBits = (startingBitsWritten + bits) - bitsWritten;
assert overflowBits < Byte.SIZE : "overflowBits(" + overflowBits + ") > " + (Byte.SIZE - 1);
if (overflowBits > 0) _writeRaw(0L, (int) overflowBits);
return this;
}
void _writeUnsigned(long value, int bits) {
assert bits >= 0 : "bits(" + bits + ") < " + 0;
assert bits < Long.SIZE : "bits(" + bits + ") > " + (Long.SIZE - 1);

View File

@ -30,6 +30,12 @@ public class ByteOutput {
return bitOutput();
}
public ByteOutput skipBytes(int bytes) {
incrementBitsWritten((long) bytes * Byte.SIZE);
buffer.writeZero(bytes);
return this;
}
long incrementBitsWritten(long bits) {
assert (bits & (Byte.SIZE - 1)) == 0;
if (bitOutput == null) return 0;