X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fdata%2FBitOutputStream.java;h=e5f4934de328730a657f32a98142d797ef536e89;hb=6846681d727a07385bcd3e0eb856f70a7e96448c;hp=c115071d1029510e71e398968ad715afe016cbdb;hpb=b34ba4499cfbca09bc794a810e460bf1c86dcd34;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java b/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java index c115071..e5f4934 100755 --- a/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java +++ b/src/main/java/eu/svjatoslav/commons/data/BitOutputStream.java @@ -1,7 +1,7 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or * modify it under the terms of version 3 of the GNU Lesser General Public License * or later as published by the Free Software Foundation. @@ -18,54 +18,43 @@ import java.io.OutputStream; public class BitOutputStream { - int currentByte; - int currentBytePointer; - - OutputStream outputStream; - - public BitOutputStream(final OutputStream outputStream) { - currentByte = 0; - currentBytePointer = 0; - this.outputStream = outputStream; - }; - - public void finishByte() throws IOException { - if (currentBytePointer != 0) { - outputStream.write(currentByte); - currentBytePointer = 0; - } - } - - public void storeBits(final int data, final int bitCount) - throws IOException { - for (int i = bitCount - 1; i >= 0; i--) { - - int mask = 1; - mask = mask << i; - - final int currentBit = data & mask; - currentByte = currentByte << 1; - - if (currentBit != 0) - currentByte = currentByte | 1; - currentBytePointer++; - - if (currentBytePointer == 8) { - currentBytePointer = 0; - outputStream.write(currentByte); - currentByte = 0; - } - } - } - - public void storeIntegerCompressed8(final int data) throws IOException { - if (data < 256) { - storeBits(0, 1); - storeBits(data, 8); - } else { - storeBits(1, 1); - storeBits(data, 32); - } - } + private final OutputStream outputStream; + private int currentByte; + private int currentBytePointer; + + public BitOutputStream(final OutputStream outputStream) { + currentByte = 0; + currentBytePointer = 0; + this.outputStream = outputStream; + } + + public void finishByte() throws IOException { + if (currentBytePointer != 0) { + outputStream.write(currentByte); + currentBytePointer = 0; + } + } + + public void storeBits(final int data, final int bitCount) + throws IOException { + for (int i = bitCount - 1; i >= 0; i--) { + + int mask = 1; + mask = mask << i; + + final int currentBit = data & mask; + currentByte = currentByte << 1; + + if (currentBit != 0) + currentByte = currentByte | 1; + currentBytePointer++; + + if (currentBytePointer == 8) { + currentBytePointer = 0; + outputStream.write(currentByte); + currentByte = 0; + } + } + } }