Debian Codegraph @

src/main/java/org/freehep/util/io/BitInputStream.java

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.EOFException;
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 /**
9 * Class to read bits from a Stream, allowing for byte synchronization. Signed,
10 * Unsigned, Booleans and Floats can be read.
11 *
12 * @author Mark Donszelmann
13 * @author Charles Loomis
14 * @version $Id: BitInputStream.java 8584 2006-08-10 23:06:37Z duns $
15 */
16 public class BitInputStream extends DecompressableInputStream {
17
18 final protected static int MASK_SIZE = 8;
19
20 final protected static int ZERO = 0;
21
22 final protected static int ONES = ~0;
23
24 final protected static int[] BIT_MASK = new int[MASK_SIZE];
25
26 final protected static int[] FIELD_MASK = new int[MASK_SIZE];
27
28 // Generate the needed masks for various bit fields and for
29 // individual bits.
30 static {
31 int tempBit = 1;
32 int tempField = 1;
33 for (int i = 0; i < MASK_SIZE; i++) {
34
35 // Set the masks.
36 BIT_MASK[i] = tempBit;
37 FIELD_MASK[i] = tempField;
38
39 // Update the temporary values.
40 tempBit <<= 1;
41 tempField <<= 1;
42 tempField++;
43 }
44 }
45
46 /**
47 * This is a prefetched byte used to construct signed and unsigned numbers
48 * with an arbitrary number of significant bits.
49 */
50 private int bits;
51
52 /**
53 * The number of valid bits remaining in the bits field.
54 */
55 private int validBits;
56
57 /**
58 * Create a Bit input stream from viven input
59 *
60 * @param in stream to read from
61 */
62 public BitInputStream(InputStream in) {
63 super(in);
64
65 bits = 0;
66 validBits = 0;
67 }
68
69 /**
70 * A utility method to fetch the next byte in preparation for constructing a
71 * bit field. There is no protection for this method; ensure that it is only
72 * called when a byte must be fetched.
73 *
74 * @throws IOException if read fails
75 */
76 protected void fetchByte() throws IOException {
77
78 bits = read();
79 if (bits < 0)
80 throw new EOFException();
81 validBits = MASK_SIZE;
82 }
83
84 /**
85 * A utility to force the next read to be byte-aligned.
86 */
87 public void byteAlign() {
88 validBits = 0;
89 }
90
91 /**
92 * Read a bit from the input stream and interpret this as a boolean value. A
93 * 1-bit is true; a 0-bit is false.
94 *
95 * @return true if read bit was 1
96 * @throws IOException if read fails
97 */
98 public boolean readBitFlag() throws IOException {
99
100 if (validBits == 0)
101 fetchByte();
102 return ((bits & BIT_MASK[--validBits]) != 0);
103 }
104
105 /**
106 * Read a signed value of n-bits from the input stream.
107 *
108 * @param n number of bits to read
109 * @return value made up of read bits
110 * @throws IOException if read fails
111 */
112 public long readSBits(int n) throws IOException {
113
114 if (n == 0)
115 return 0;
116 int value = (readBitFlag()) ? ONES : ZERO;
117 value <<= (--n);
118 return (value | readUBits(n));
119 }
120
121 /**
122 * Read a float value of n-bits from the stream.
123 *
124 * @param n number of bits to read
125 * @return value made up of read bits
126 * @throws IOException if read fails
127 */
128 public float readFBits(int n) throws IOException {
129
130 if (n == 0)
131 return 0.0f;
132 return ((float) readSBits(n)) / 0x1000;
133 }
134
135 /**
136 * Read an unsigned value of n-bits from the input stream.
137 *
138 * @param n number of bits to read
139 * @return value made up of read bits
140 * @throws IOException if read fails
141 */
142 public long readUBits(int n) throws IOException {
143
144 long value = ZERO;
145 while (n > 0) {
146
147 // Take the needed bits or the number which are valid
148 // whichever is less.
149 if (validBits == 0)
150 fetchByte();
151 int nbits = (n > validBits) ? validBits : n;
152
153 // Take the bits and update the counters.
154 int temp = ((bits >> (validBits - nbits)) & FIELD_MASK[nbits - 1]);
155 validBits -= nbits;
156 n -= nbits;
157
158 // Shift the value up to accomodate new bits.
159 value <<= nbits;
160 value |= temp;
161 }
162 return value;
163 }
164 }