reading from the cache doesn't need to throw an exception
@@ -89,15 +89,7 @@ public long readBits(final int count) throws IOException {
if (bitsCachedSize < count) {
return processBitsGreater57(count);
}
- final long bitsOut;
- if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
- bitsOut = (bitsCached & MASKS[count]);
- bitsCached >>>= count;
- } else {
- bitsOut = (bitsCached >> (bitsCachedSize - count)) & MASKS[count];
- }
- bitsCachedSize -= count;
- return bitsOut;
+ return readCachedBits(count);
}
/**
@@ -124,13 +116,12 @@ public long bitsAvailable() throws IOException {
/**
* Drops bits until the next bits will be read from a byte boundary.
- * @throws IOException if reading the remaining bits to the next byte boundary fails
* @since 1.16
*/
- public void alignWithByteBoundary() throws IOException {
+ public void alignWithByteBoundary() {
int toSkip = bitsCachedSize % 8;
if (toSkip > 0) {
- readBits(toSkip);
+ readCachedBits(toSkip);
}
}
@@ -162,6 +153,18 @@ private long processBitsGreater57(final int count) throws IOException {
return bitsOut;
}
+ private long readCachedBits(int count) {
+ final long bitsOut;
+ if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
+ bitsOut = (bitsCached & MASKS[count]);
+ bitsCached >>>= count;
+ } else {
+ bitsOut = (bitsCached >> (bitsCachedSize - count)) & MASKS[count];
+ }
+ bitsCachedSize -= count;
+ return bitsOut;
+ }
+
/**
* Fills the cache up to 56 bits
* @param count
