Added support for marking read position for debugging

This commit is contained in:
Collin Smith 2020-08-12 17:22:55 -07:00
parent d70668d11d
commit cafe8679b4
3 changed files with 27 additions and 4 deletions

View File

@ -172,6 +172,7 @@ public class BitInput {
}
long incrementBitsRead(long bits) {
byteInput.updateMark();
if ((bitsRead += bits) > numBits) {
bitsRead = numBits;
throw new EndOfInput();
@ -181,6 +182,7 @@ public class BitInput {
}
long decrementBitsRead(long bits) {
byteInput.updateMark();
if ((bitsRead -= bits) < 0) {
assert false : "bitsRead(" + bitsRead + ") < " + 0;
bitsRead = 0;

View File

@ -28,11 +28,27 @@ public class ByteInput {
}
private final ByteBuf buffer;
private final int offset;
private int mark;
private BitInput bitInput;
ByteInput(ByteBuf buffer) {
this(buffer, 0);
}
ByteInput(ByteBuf buffer, int offset) {
assert buffer.isReadOnly() : "buffer should be tagged ByteBuf#asReadOnly()";
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) {
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);
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) {
updateMark();
assert (bits & (Byte.SIZE - 1)) == 0;
if (bitInput == null) return 0;
return bitInput.incrementBitsRead(bits);
@ -177,7 +196,9 @@ public class ByteInput {
long decrementBitsRead(long bits) {
assert (bits & (Byte.SIZE - 1)) == 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;
}
/**

View File

@ -12,8 +12,8 @@ public class InvalidFormat extends RuntimeException {
}
public InvalidFormat(ByteInput in, String message, Throwable cause) {
super(message + " @0x" + Integer.toHexString(in.bytesRead()), cause);
this.offset = in.bytesRead();
super(message + " +0x" + Integer.toHexString(in.mark()), cause);
this.offset = in.mark();
}
@Deprecated