From cafe8679b410ec98abd2986f3f19afc4edd347a1 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Wed, 12 Aug 2020 17:22:55 -0700 Subject: [PATCH] Added support for marking read position for debugging --- core/src/com/riiablo/io/BitInput.java | 2 ++ core/src/com/riiablo/io/ByteInput.java | 25 ++++++++++++++++++++-- core/src/com/riiablo/io/InvalidFormat.java | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/core/src/com/riiablo/io/BitInput.java b/core/src/com/riiablo/io/BitInput.java index 70da63cd..0fa57779 100644 --- a/core/src/com/riiablo/io/BitInput.java +++ b/core/src/com/riiablo/io/BitInput.java @@ -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; diff --git a/core/src/com/riiablo/io/ByteInput.java b/core/src/com/riiablo/io/ByteInput.java index 81747935..db860dec 100644 --- a/core/src/com/riiablo/io/ByteInput.java +++ b/core/src/com/riiablo/io/ByteInput.java @@ -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; } /** diff --git a/core/src/com/riiablo/io/InvalidFormat.java b/core/src/com/riiablo/io/InvalidFormat.java index 0f38d4bc..935273b0 100644 --- a/core/src/com/riiablo/io/InvalidFormat.java +++ b/core/src/com/riiablo/io/InvalidFormat.java @@ -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