From 1b51043d7d63d3092f6e324e549138941c5795de Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sun, 9 Aug 2020 12:48:33 -0700 Subject: [PATCH] Moved UBITS consts to BitConstants --- core/src/com/riiablo/io/BitConstants.java | 10 +++++++ core/src/com/riiablo/io/BitInput.java | 36 ++++++++++------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/core/src/com/riiablo/io/BitConstants.java b/core/src/com/riiablo/io/BitConstants.java index 7d7523f2..45f936a9 100644 --- a/core/src/com/riiablo/io/BitConstants.java +++ b/core/src/com/riiablo/io/BitConstants.java @@ -3,6 +3,16 @@ package com.riiablo.io; public class BitConstants { private BitConstants() {} + static final int MAX_ULONG_BITS = Long.SIZE - 1; + static final int MAX_UINT_BITS = Integer.SIZE - 1; + static final int MAX_USHORT_BITS = Short.SIZE - 1; + static final int MAX_UBYTE_BITS = Byte.SIZE - 1; + static final int MAX_UNSIGNED_BITS = MAX_ULONG_BITS; + + static final int MAX_SAFE_CACHED_BITS = Long.SIZE - Byte.SIZE; + + static final int BYTE_SHIFT = Integer.bitCount(Byte.SIZE - 1); + static final long[] UNSIGNED_MASKS = new long[Long.SIZE]; static { for (int i = 1; i < Long.SIZE; i++) { diff --git a/core/src/com/riiablo/io/BitInput.java b/core/src/com/riiablo/io/BitInput.java index e2b6adbb..cd877476 100644 --- a/core/src/com/riiablo/io/BitInput.java +++ b/core/src/com/riiablo/io/BitInput.java @@ -2,6 +2,8 @@ package com.riiablo.io; import org.apache.commons.lang3.StringUtils; +import static com.riiablo.io.BitConstants.MAX_SAFE_CACHED_BITS; + /** * Wraps a {@link ByteInput} to support reading sequences of bits and * supporting {@link #align() re-aligning} the byte stream to read sequences of @@ -17,14 +19,6 @@ public class BitInput { return ByteInput.wrap(bytes).unalign(); } - private static final int MAX_ULONG_BITS = Long.SIZE - 1; - private static final int MAX_UINT_BITS = Integer.SIZE - 1; - private static final int MAX_USHORT_BITS = Short.SIZE - 1; - private static final int MAX_UBYTE_BITS = Byte.SIZE - 1; - private static final int MAX_UNSIGNED_BITS = MAX_ULONG_BITS; - - private static final int MAX_SAFE_CACHED_BITS = Long.SIZE - Byte.SIZE; - private static final long[] MASKS = BitConstants.UNSIGNED_MASKS; private final ByteInput byteInput; @@ -196,11 +190,11 @@ public class BitInput { } /** - * Reads up to {@value #MAX_UNSIGNED_BITS} bits as unsigned and casts the - * result into a {@code long}. + * Reads up to {@value BitConstants#MAX_UNSIGNED_BITS} bits as unsigned and + * casts the result into a {@code long}. *

- *

{@code bits} should be in [0, {@value #MAX_UNSIGNED_BITS}]. - *

Reading {@code 0} bits will always return {@code 0}. + *

{@code bits} should be in [0, {@value BitConstants##MAX_UNSIGNED_BITS}] + *

Reading {@code 0} bits will always return {@code 0} */ long readUnsigned(int bits) { assert bits >= 0 : "bits(" + bits + ") < " + 0; @@ -215,7 +209,7 @@ public class BitInput { /** * Ensures {@link #cache} contains at least n bits, up to - * {@value #MAX_SAFE_CACHED_BITS} bits due to overflow. + * {@value BitConstants#MAX_SAFE_CACHED_BITS} bits due to overflow. */ private int ensureCache(int bits) { assert bits > 0 : "bits(" + bits + ") < " + 1; @@ -302,8 +296,8 @@ public class BitInput { } /** - * Reads up to {@value #MAX_UBYTE_BITS} bits as unsigned and casts the result - * into a {@code byte}. + * Reads up to {@value BitConstants##MAX_UBYTE_BITS} bits as unsigned and + * casts the result into a {@code byte}. */ public byte read7u(int bits) { BitConstraints.validate7u(bits); @@ -313,8 +307,8 @@ public class BitInput { } /** - * Reads up to {@value #MAX_USHORT_BITS} bits as unsigned and casts the - * result into a {@code short}. + * Reads up to {@value BitConstants#MAX_USHORT_BITS} bits as unsigned and + * casts the result into a {@code short}. */ public short read15u(int bits) { BitConstraints.validate15u(bits); @@ -324,8 +318,8 @@ public class BitInput { } /** - * Reads up to {@value #MAX_UINT_BITS} bits as unsigned and casts the result - * into a {@code int}. + * Reads up to {@value BitConstants#MAX_UINT_BITS} bits as unsigned and casts + * the result into a {@code int}. */ public int read31u(int bits) { BitConstraints.validate31u(bits); @@ -335,8 +329,8 @@ public class BitInput { } /** - * Reads up to {@value #MAX_ULONG_BITS} bits as unsigned and casts the result - * into a {@code long}. + * Reads up to {@value BitConstants#MAX_ULONG_BITS} bits as unsigned and + * casts the result into a {@code long}. */ public long read63u(int bits) { BitConstraints.validate63u(bits);