KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > io > NBitInputStream


1 /**
2   * Copyright (c) 2003, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.io;
32
33 import java.io.InputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * This is an n-bit input stream. This means that you can read chunks of data
38  * as any number of bits, not just 8 bits like the regular input stream. Just set the
39  * number of bits that you would like to read on each call. The default is 8.
40  *
41  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
42  * @version $Revision: 1.4 $
43  */

44 public class NBitInputStream
45 {
46     private int bitsInChunk;
47     private InputStream JavaDoc in;
48
49     private int currentByte;
50     private int bitsLeftInCurrentByte;
51
52     /**
53      * Constructor.
54      *
55      * @param is The input stream to read from.
56      */

57     public NBitInputStream( InputStream JavaDoc is )
58     {
59         in = is;
60         bitsLeftInCurrentByte = 0;
61         bitsInChunk = 8;
62     }
63
64     /**
65      * This will unread some data.
66      *
67      * @param data The data to put back into the stream.
68      */

69     public void unread( long data )
70     {
71         data <<= bitsLeftInCurrentByte;
72         currentByte |= data;
73         bitsLeftInCurrentByte += bitsInChunk;
74     }
75
76     /**
77      * This will read the next n bits from the stream and return the unsigned
78      * value of those bits.
79      *
80      * @return The next n bits from the stream.
81      *
82      * @throws IOException If there is an error reading from the underlying stream.
83      */

84     public long read() throws IOException JavaDoc
85     {
86         long retval = 0;
87         for( int i=0; i<bitsInChunk && retval != -1; i++ )
88         {
89             if( bitsLeftInCurrentByte == 0 )
90             {
91                 currentByte = in.read();
92                 bitsLeftInCurrentByte = 8;
93             }
94             if( currentByte == -1 )
95             {
96                 retval = -1;
97             }
98             else
99             {
100                 retval <<= 1;
101                 retval |= ((currentByte >> (bitsLeftInCurrentByte-1))&0x1);
102                 bitsLeftInCurrentByte--;
103             }
104         }
105         return retval;
106     }
107
108     /** Getter for property bitsToRead.
109      * @return Value of property bitsToRead.
110      */

111     public int getBitsInChunk()
112     {
113         return bitsInChunk;
114     }
115
116     /** Setter for property bitsToRead.
117      * @param bitsInChunkValue New value of property bitsToRead.
118      */

119     public void setBitsInChunk(int bitsInChunkValue)
120     {
121         bitsInChunk = bitsInChunkValue;
122     }
123
124 }
Popular Tags