| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package org.apache.commons.compress.utils; |
| 20 |
|
| 21 |
import java.io.Closeable; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.InputStream; |
| 24 |
import java.nio.ByteOrder; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class BitInputStream implements Closeable { |
| 33 |
private static final int MAXIMUM_CACHE_SIZE = 63; |
| 34 |
private static final long[] MASKS = new long[MAXIMUM_CACHE_SIZE + 1]; |
| 35 |
|
| 36 |
static { |
| 37 |
for (int i = 1; i <= MAXIMUM_CACHE_SIZE; i++) { |
| 38 |
MASKS[i] = (MASKS[i - 1] << 1) + 1; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
private final org.apache.commons.io.input.BoundedInputStream in; |
| 43 |
private final ByteOrder byteOrder; |
| 44 |
private long bitsCached; |
| 45 |
private int bitsCachedSize; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public BitInputStream(final InputStream in, final ByteOrder byteOrder) { |
| 54 |
this.in = org.apache.commons.io.input.BoundedInputStream.builder().setInputStream(in).asSupplier().get(); |
| 55 |
this.byteOrder = byteOrder; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public void alignWithByteBoundary() { |
| 64 |
final int toSkip = bitsCachedSize % Byte.SIZE; |
| 65 |
if (toSkip > 0) { |
| 66 |
readCachedBits(toSkip); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public long bitsAvailable() throws IOException { |
| 79 |
return bitsCachedSize + (long) Byte.SIZE * in.available(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public int bitsCached() { |
| 89 |
return bitsCachedSize; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public void clearBitCache() { |
| 96 |
bitsCached = 0; |
| 97 |
bitsCachedSize = 0; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void close() throws IOException { |
| 102 |
in.close(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private boolean ensureCache(final int count) throws IOException { |
| 113 |
while (bitsCachedSize < count && bitsCachedSize < 57) { |
| 114 |
final long nextByte = in.read(); |
| 115 |
if (nextByte < 0) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
if (byteOrder == ByteOrder.LITTLE_ENDIAN) { |
| 119 |
bitsCached |= nextByte << bitsCachedSize; |
| 120 |
} else { |
| 121 |
bitsCached <<= Byte.SIZE; |
| 122 |
bitsCached |= nextByte; |
| 123 |
} |
| 124 |
bitsCachedSize += Byte.SIZE; |
| 125 |
} |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public long getBytesRead() { |
| 139 |
return in.getCount(); |
| 140 |
} |
| 141 |
|
| 142 |
private long processBitsGreater57(final int count) throws IOException { |
| 143 |
final long bitsOut; |
| 144 |
final int overflowBits; |
| 145 |
long overflow = 0L; |
| 146 |
|
| 147 |
|
| 148 |
final int bitsToAddCount = count - bitsCachedSize; |
| 149 |
overflowBits = Byte.SIZE - bitsToAddCount; |
| 150 |
final long nextByte = in.read(); |
| 151 |
if (nextByte < 0) { |
| 152 |
return nextByte; |
| 153 |
} |
| 154 |
if (byteOrder == ByteOrder.LITTLE_ENDIAN) { |
| 155 |
final long bitsToAdd = nextByte & MASKS[bitsToAddCount]; |
| 156 |
bitsCached |= bitsToAdd << bitsCachedSize; |
| 157 |
overflow = nextByte >>> bitsToAddCount & MASKS[overflowBits]; |
| 158 |
} else { |
| 159 |
bitsCached <<= bitsToAddCount; |
| 160 |
final long bitsToAdd = nextByte >>> overflowBits & MASKS[bitsToAddCount]; |
| 161 |
bitsCached |= bitsToAdd; |
| 162 |
overflow = nextByte & MASKS[overflowBits]; |
| 163 |
} |
| 164 |
bitsOut = bitsCached & MASKS[count]; |
| 165 |
bitsCached = overflow; |
| 166 |
bitsCachedSize = overflowBits; |
| 167 |
return bitsOut; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public int readBit() throws IOException { |
| 178 |
return (int) readBits(1); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public long readBits(final int count) throws IOException { |
| 190 |
if (count < 0 || count > MAXIMUM_CACHE_SIZE) { |
| 191 |
throw new IOException("count must not be negative or greater than " + MAXIMUM_CACHE_SIZE); |
| 192 |
} |
| 193 |
if (ensureCache(count)) { |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
if (bitsCachedSize < count) { |
| 198 |
return processBitsGreater57(count); |
| 199 |
} |
| 200 |
return readCachedBits(count); |
| 201 |
} |
| 202 |
|
| 203 |
private long readCachedBits(final int count) { |
| 204 |
final long bitsOut; |
| 205 |
if (byteOrder == ByteOrder.LITTLE_ENDIAN) { |
| 206 |
bitsOut = bitsCached & MASKS[count]; |
| 207 |
bitsCached >>>= count; |
| 208 |
} else { |
| 209 |
bitsOut = bitsCached >> bitsCachedSize - count & MASKS[count]; |
| 210 |
} |
| 211 |
bitsCachedSize -= count; |
| 212 |
return bitsOut; |
| 213 |
} |
| 214 |
|
| 215 |
} |