From afd7bafa12d4fa1c8ab58ae79ad87d735056f7c1 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sat, 8 Aug 2020 13:17:59 -0700 Subject: [PATCH] Fixed skipBits multi-byte using unaligned bit stream --- core/src/com/riiablo/io/nio/BitInput.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/com/riiablo/io/nio/BitInput.java b/core/src/com/riiablo/io/nio/BitInput.java index 6bc3bf3a..e3c2bd49 100644 --- a/core/src/com/riiablo/io/nio/BitInput.java +++ b/core/src/com/riiablo/io/nio/BitInput.java @@ -146,11 +146,20 @@ public class BitInput { if (bits < 0) throw new IllegalArgumentException("bits(" + bits + ") < " + 0); if (bits == 0) return this; + // aligns bit stream for multi-byte skipping + assert bitsCached < Byte.SIZE : "bitsCached(" + bitsCached + ") > " + (Byte.SIZE - 1); + if (bits >= bitsCached) { + bits -= bitsCached; + align(); + } + + // skips multiple bytes final long startingBitsRead = bitsRead; final long bytes = bits / Byte.SIZE; assert bytes <= Integer.MAX_VALUE : "bytes(" + bytes + ") > Integer.MAX_VALUE"; if (bytes > 0) align().skipBytes((int) bytes); + // skips overflow bits final long overflowBits = (startingBitsRead + bits) - bitsRead; // checks single byte, multi-byte and expected max value assert bytes != 0 || overflowBits < Byte.SIZE : "overflowBits(" + overflowBits + ") > " + (Byte.SIZE - 1);