X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fdata%2FBitInputStream.java;h=f41585572cd1b0edaee6701e958cc09879f7039b;hb=965adace2b47642e2ca84f499cc32683c12059c6;hp=6564417bfa6dc4c9c5c0a997f884936b554a9d52;hpb=cf965fda534cc562368c9f2a3f34475e2519fcdc;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java b/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java index 6564417..f415855 100755 --- a/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/BitInputStream.java @@ -1,62 +1,54 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public License - * as published by the Free Software Foundation. + * modify it under the terms of version 3 of the GNU Lesser General Public License + * or later as published by the Free Software Foundation. */ package eu.svjatoslav.commons.data; -/** - * Read individual bits from the input stream. - */ import java.io.IOException; import java.io.InputStream; +/** + * Read individual bits from the input stream. + */ public class BitInputStream { - int currentByte; - int currentBytePointer = -1; - - InputStream inputStream; - - public BitInputStream(final InputStream inputStream) { - this.inputStream = inputStream; - } + private final InputStream inputStream; + private int currentByte; + private int currentBytePointer = -1; - public int readBits(final int bitCount) throws IOException { + public BitInputStream(final InputStream inputStream) { + this.inputStream = inputStream; + } - int readableByte = 0; - for (int i = 0; i < bitCount; i++) { + public int readBits(final int bitCount) throws IOException { - readableByte = readableByte << 1; + int readableByte = 0; + for (int i = 0; i < bitCount; i++) { - if (currentBytePointer == -1) { - currentBytePointer = 7; - currentByte = inputStream.read(); - } + readableByte = readableByte << 1; - int mask = 1; - mask = mask << currentBytePointer; + if (currentBytePointer == -1) { + currentBytePointer = 7; + currentByte = inputStream.read(); + } - final int currentBit = currentByte & mask; + int mask = 1; + mask = mask << currentBytePointer; - if (currentBit != 0) - readableByte = readableByte | 1; + final int currentBit = currentByte & mask; - currentBytePointer--; - } - return readableByte; - } + if (currentBit != 0) + readableByte = readableByte | 1; - public int readIntegerCompressed8() throws IOException { - if (readBits(1) == 0) - return readBits(8); - else - return readBits(32); - } + currentBytePointer--; + } + return readableByte; + } }