Tightened up field visibility

Tightened up field visibility
ByteInput#bitInput set via bitInput(BitInput) which enforces invariants
This commit is contained in:
Collin Smith 2020-08-08 14:08:26 -07:00
parent afd7bafa12
commit 20ae623fe7
2 changed files with 16 additions and 7 deletions

View File

@ -22,10 +22,9 @@ public class BitInput {
}
}
final ByteInput byteInput;
final long numBits;
long bitsRead;
private final ByteInput byteInput;
private final long numBits;
private long bitsRead;
private int bitsCached;
private long cache;
@ -48,6 +47,10 @@ public class BitInput {
this.numBits = numBits;
}
ByteInput byteInput() {
return byteInput;
}
public int bytesRead() {
return byteInput.bytesRead();
}
@ -136,7 +139,7 @@ public class BitInput {
// length should include the last byte that bits belong (round to ceil)
final long numBytes = (numBits - bitsCached + Byte.SIZE - 1) / Byte.SIZE;
final ByteInput byteInput = this.byteInput.readSlice(numBytes);
return byteInput.bitInput = new BitInput(byteInput, bitsCached, cache, numBits);
return byteInput.bitInput(new BitInput(byteInput, bitsCached, cache, numBits));
}
/**

View File

@ -17,13 +17,19 @@ public class ByteInput {
return bytes == null ? emptyByteInput() : new ByteInput(Unpooled.wrappedBuffer(bytes));
}
final ByteBuf buffer;
BitInput bitInput;
private final ByteBuf buffer;
private BitInput bitInput;
ByteInput(ByteBuf buffer) {
this.buffer = buffer;
}
BitInput bitInput(BitInput bitInput) {
assert this.bitInput == null : "this.bitInput(" + this.bitInput + ") != null";
assert bitInput.byteInput() == this : "bitInput.byteInput()(" + bitInput.byteInput() + ") != this(" + this + ")";
return this.bitInput = bitInput;
}
public int bytesRead() {
return buffer.readerIndex();
}