2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.data;
8 import java.io.IOException;
9 import java.io.InputStream;
12 * Read individual bits from the input stream.
14 public class BitInputStream {
16 private final InputStream inputStream;
17 private int currentByte;
18 private int currentBytePointer = -1;
20 public BitInputStream(final InputStream inputStream) {
21 this.inputStream = inputStream;
24 public int readBits(final int bitCount) throws IOException {
27 for (int i = 0; i < bitCount; i++) {
29 readableByte = readableByte << 1;
31 if (currentBytePointer == -1) {
32 currentBytePointer = 7;
33 currentByte = inputStream.read();
37 mask = mask << currentBytePointer;
39 final int currentBit = currentByte & mask;
42 readableByte = readableByte | 1;