mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-03-06 07:30:41 +07:00
Changed ByteInput to lazily allocate BitInput object
BitInput not always required, mostly only ByteInput is used Removed specialized BitInput constructors from ByteInput ByteInput#unalign will allocate a BitInput of readableBytes size BitInput#readSlice will allocate a BitInput of specified size
This commit is contained in:
parent
673b437c61
commit
3e6e2c05ce
@ -14,16 +14,18 @@ public class BitInput implements Aligned, AlignedReader, UnalignedReader {
|
||||
}
|
||||
|
||||
final ByteInput byteInput;
|
||||
// final ByteBuf buffer;
|
||||
final long numBits;
|
||||
long bitsRead;
|
||||
|
||||
private int bitsCached;
|
||||
private long cache;
|
||||
|
||||
BitInput(ByteInput byteInput) {
|
||||
this(byteInput, 0, 0L, (long) byteInput.bytesRemaining() * Byte.SIZE);
|
||||
}
|
||||
|
||||
BitInput(ByteInput byteInput, int bitsCached, long cache, long numBits) {
|
||||
this.byteInput = byteInput;
|
||||
// this.buffer = byteInput.buffer;
|
||||
this.bitsCached = bitsCached;
|
||||
this.cache = cache;
|
||||
this.numBits = numBits;
|
||||
@ -109,7 +111,8 @@ public class BitInput implements Aligned, AlignedReader, UnalignedReader {
|
||||
|
||||
// length should include the last byte that bits belong (round to ceil)
|
||||
final long numBytes = (numBits - bitsCached + Byte.SIZE - 1) / Byte.SIZE;
|
||||
return byteInput.readSlice(numBytes, bitsCached, cache, numBits).unalign();
|
||||
final ByteInput byteInput = this.byteInput.readSlice(numBytes);
|
||||
return byteInput.bitInput = new BitInput(byteInput, bitsCached, cache, numBits);
|
||||
}
|
||||
|
||||
public BitInput discard(long bits) {
|
||||
|
@ -16,16 +16,11 @@ public class ByteInput implements Aligned, AlignedReader {
|
||||
return bytes == null ? emptyByteInput() : new ByteInput(Unpooled.wrappedBuffer(bytes));
|
||||
}
|
||||
|
||||
final BitInput bitInput;
|
||||
final ByteBuf buffer;
|
||||
BitInput bitInput;
|
||||
|
||||
ByteInput(ByteBuf buffer) {
|
||||
this(buffer, 0, 0L, (long) buffer.readableBytes() * Byte.SIZE);
|
||||
}
|
||||
|
||||
ByteInput(ByteBuf buffer, int bitsCached, long cache, long numBits) {
|
||||
this.buffer = buffer;
|
||||
this.bitInput = new BitInput(this, bitsCached, cache, numBits);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -49,6 +44,8 @@ public class ByteInput implements Aligned, AlignedReader {
|
||||
}
|
||||
|
||||
public BitInput unalign() {
|
||||
// FIXME: should bitInput.bitsRead = (long) bytesRead * Byte.SIZE
|
||||
if (bitInput == null) this.bitInput = new BitInput(this);
|
||||
return bitInput;
|
||||
}
|
||||
|
||||
@ -99,13 +96,9 @@ public class ByteInput implements Aligned, AlignedReader {
|
||||
}
|
||||
|
||||
public ByteInput readSlice(long numBytes) {
|
||||
return readSlice(numBytes, 0, 0L, numBytes * Byte.SIZE);
|
||||
}
|
||||
|
||||
public ByteInput readSlice(long numBytes, int bitsCached, long cache, long numBits) {
|
||||
assert numBytes <= Integer.MAX_VALUE : "ByteBuf only supports int length";
|
||||
final ByteBuf slice = buffer.readSlice((int) numBytes);
|
||||
return new ByteInput(slice, bitsCached, cache, numBits);
|
||||
return new ByteInput(slice);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user