KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * This is an n-bit output stream. This means that you write data in n-bit chunks.
38  *
39  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
40  * @version $Revision: 1.4 $
41  */

42 public class NBitOutputStream
43 {
44     private int bitsInChunk;
45     private OutputStream JavaDoc out;
46
47     private int currentByte;
48     private int positionInCurrentByte;
49
50     /**
51      * Constructor.
52      *
53      * @param os The output stream to write to.
54      */

55     public NBitOutputStream( OutputStream JavaDoc os )
56     {
57         out = os;
58         currentByte = 0;
59         positionInCurrentByte = 7;
60     }
61
62     /**
63      * This will write the next n-bits to the stream.
64      *
65      * @param chunk The next chunk of data to write.
66      *
67      * @throws IOException If there is an error writing the chunk.
68      */

69     public void write( long chunk ) throws IOException JavaDoc
70     {
71         long bitToWrite;
72         for( int i=(bitsInChunk-1); i>=0; i-- )
73         {
74             bitToWrite = (chunk >> i) & 0x1;
75             bitToWrite <<= positionInCurrentByte;
76             currentByte |= bitToWrite;
77             positionInCurrentByte--;
78             if( positionInCurrentByte < 0 )
79             {
80                 out.write( currentByte );
81                 currentByte = 0;
82                 positionInCurrentByte = 7;
83             }
84         }
85     }
86
87     /**
88      * This will close the stream.
89      *
90      * @throws IOException if there is an error closing the stream.
91      */

92     public void close() throws IOException JavaDoc
93     {
94         if( positionInCurrentByte < 7 )
95         {
96             out.write( currentByte );
97         }
98     }
99
100     /** Getter for property bitsToRead.
101      * @return Value of property bitsToRead.
102      */

103     public int getBitsInChunk()
104     {
105         return bitsInChunk;
106     }
107
108     /** Setter for property bitsToRead.
109      * @param bitsInChunkValue New value of property bitsToRead.
110      */

111     public void setBitsInChunk(int bitsInChunkValue)
112     {
113         bitsInChunk = bitsInChunkValue;
114     }
115
116 }
Popular Tags