Java Reference
In-Depth Information
1 // BitInputStream class: Bit-input stream wrapper class.
2 //
3 // CONSTRUCTION: with an open InputStream.
4 //
5 // ******************PUBLIC OPERATIONS***********************
6 // int readBit( ) --> Read one bit as a 0 or 1
7 // void close( ) --> Close underlying stream
8
9 public class BitInputStream
10 {
11 public BitInputStream( InputStream is )
12 {
13 in = is;
14 bufferPos = BitUtils.BITS_PER_BYTES;
15 }
16
17 public int readBit( ) throws IOException
18 {
19 if( bufferPos == BitUtils.BITS_PER_BYTES )
20 {
21 buffer = in.read( );
22 if( buffer == -1 )
23 return -1;
24 bufferPos = 0;
25 }
26
27 return getBit( buffer, bufferPos++ );
28 }
29
30 public void close( ) throws IOException
31 {
32 in.close( );
33 }
34
35 private static int getBit( int pack, int pos )
36 {
37 return ( pack & ( 1 << pos ) ) != 0 ? 1 : 0;
38 }
39
40 private InputStream in;
41 private int buffer;
42 private int bufferPos;
43 }
figure 12.14
The BitInputStream
class
Search WWH ::




Custom Search