2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, 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 * Write individual bits to the output stream.
16 import java.io.IOException;
17 import java.io.OutputStream;
19 public class BitOutputStream {
22 int currentBytePointer;
24 OutputStream outputStream;
26 public BitOutputStream(final OutputStream outputStream) {
28 currentBytePointer = 0;
29 this.outputStream = outputStream;
32 public void finishByte() throws IOException {
33 if (currentBytePointer != 0) {
34 outputStream.write(currentByte);
35 currentBytePointer = 0;
39 public void storeBits(final int data, final int bitCount)
41 for (int i = bitCount - 1; i >= 0; i--) {
46 final int currentBit = data & mask;
47 currentByte = currentByte << 1;
50 currentByte = currentByte | 1;
53 if (currentBytePointer == 8) {
54 currentBytePointer = 0;
55 outputStream.write(currentByte);
61 public void storeIntegerCompressed8(final int data) throws IOException {