mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-15 10:17:50 +07:00
Added support for marking read position for debugging
This commit is contained in:
@ -172,6 +172,7 @@ public class BitInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long incrementBitsRead(long bits) {
|
long incrementBitsRead(long bits) {
|
||||||
|
byteInput.updateMark();
|
||||||
if ((bitsRead += bits) > numBits) {
|
if ((bitsRead += bits) > numBits) {
|
||||||
bitsRead = numBits;
|
bitsRead = numBits;
|
||||||
throw new EndOfInput();
|
throw new EndOfInput();
|
||||||
@ -181,6 +182,7 @@ public class BitInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long decrementBitsRead(long bits) {
|
long decrementBitsRead(long bits) {
|
||||||
|
byteInput.updateMark();
|
||||||
if ((bitsRead -= bits) < 0) {
|
if ((bitsRead -= bits) < 0) {
|
||||||
assert false : "bitsRead(" + bitsRead + ") < " + 0;
|
assert false : "bitsRead(" + bitsRead + ") < " + 0;
|
||||||
bitsRead = 0;
|
bitsRead = 0;
|
||||||
|
@ -28,11 +28,27 @@ public class ByteInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final ByteBuf buffer;
|
private final ByteBuf buffer;
|
||||||
|
private final int offset;
|
||||||
|
private int mark;
|
||||||
private BitInput bitInput;
|
private BitInput bitInput;
|
||||||
|
|
||||||
ByteInput(ByteBuf buffer) {
|
ByteInput(ByteBuf buffer) {
|
||||||
|
this(buffer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
ByteInput(ByteBuf buffer, int offset) {
|
||||||
assert buffer.isReadOnly() : "buffer should be tagged ByteBuf#asReadOnly()";
|
assert buffer.isReadOnly() : "buffer should be tagged ByteBuf#asReadOnly()";
|
||||||
this.buffer = buffer;
|
this.buffer = buffer;
|
||||||
|
this.offset = offset;
|
||||||
|
updateMark();
|
||||||
|
}
|
||||||
|
|
||||||
|
int updateMark() {
|
||||||
|
return mark = offset + buffer.readerIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int mark() {
|
||||||
|
return mark;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,8 +167,10 @@ public class ByteInput {
|
|||||||
*/
|
*/
|
||||||
public ByteInput readSlice(long numBytes) {
|
public ByteInput readSlice(long numBytes) {
|
||||||
assert numBytes <= Integer.MAX_VALUE : "ByteBuf only supports int length";
|
assert numBytes <= Integer.MAX_VALUE : "ByteBuf only supports int length";
|
||||||
|
final int mark = updateMark(); // updates mark to start offset of slice
|
||||||
final ByteBuf slice = buffer.readSlice((int) numBytes);
|
final ByteBuf slice = buffer.readSlice((int) numBytes);
|
||||||
return new ByteInput(slice);
|
updateMark(); // updates mark to end position of slice
|
||||||
|
return new ByteInput(slice, mark);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -169,6 +187,7 @@ public class ByteInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long incrementBitsRead(long bits) {
|
long incrementBitsRead(long bits) {
|
||||||
|
updateMark();
|
||||||
assert (bits & (Byte.SIZE - 1)) == 0;
|
assert (bits & (Byte.SIZE - 1)) == 0;
|
||||||
if (bitInput == null) return 0;
|
if (bitInput == null) return 0;
|
||||||
return bitInput.incrementBitsRead(bits);
|
return bitInput.incrementBitsRead(bits);
|
||||||
@ -177,7 +196,9 @@ public class ByteInput {
|
|||||||
long decrementBitsRead(long bits) {
|
long decrementBitsRead(long bits) {
|
||||||
assert (bits & (Byte.SIZE - 1)) == 0;
|
assert (bits & (Byte.SIZE - 1)) == 0;
|
||||||
if (bitInput == null) return 0;
|
if (bitInput == null) return 0;
|
||||||
return bitInput.decrementBitsRead(bits);
|
long bitsRead = bitInput.decrementBitsRead(bits);
|
||||||
|
assert mark == buffer.readerIndex() : "mark(" + mark + ") != buffer.readerIndex(" + buffer.readerIndex() + ")";
|
||||||
|
return bitsRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,8 +12,8 @@ public class InvalidFormat extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InvalidFormat(ByteInput in, String message, Throwable cause) {
|
public InvalidFormat(ByteInput in, String message, Throwable cause) {
|
||||||
super(message + " @0x" + Integer.toHexString(in.bytesRead()), cause);
|
super(message + " +0x" + Integer.toHexString(in.mark()), cause);
|
||||||
this.offset = in.bytesRead();
|
this.offset = in.mark();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
|
Reference in New Issue
Block a user