2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.data;
13 import java.io.IOException;
14 import java.io.InputStream;
17 * Read individual bits from the input stream.
19 public class BitInputStream {
21 private final InputStream inputStream;
22 private int currentByte;
23 private int currentBytePointer = -1;
25 public BitInputStream(final InputStream inputStream) {
26 this.inputStream = inputStream;
29 public int readBits(final int bitCount) throws IOException {
32 for (int i = 0; i < bitCount; i++) {
34 readableByte = readableByte << 1;
36 if (currentBytePointer == -1) {
37 currentBytePointer = 7;
38 currentByte = inputStream.read();
42 mask = mask << currentBytePointer;
44 final int currentBit = currentByte & mask;
47 readableByte = readableByte | 1;