001: /**
002: * Copyright (c) 2003, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.io;
031:
032: import java.io.OutputStream;
033: import java.io.IOException;
034:
035: /**
036: * This is an n-bit output stream. This means that you write data in n-bit chunks.
037: *
038: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
039: * @version $Revision: 1.4 $
040: */
041: public class NBitOutputStream {
042: private int bitsInChunk;
043: private OutputStream out;
044:
045: private int currentByte;
046: private int positionInCurrentByte;
047:
048: /**
049: * Constructor.
050: *
051: * @param os The output stream to write to.
052: */
053: public NBitOutputStream(OutputStream os) {
054: out = os;
055: currentByte = 0;
056: positionInCurrentByte = 7;
057: }
058:
059: /**
060: * This will write the next n-bits to the stream.
061: *
062: * @param chunk The next chunk of data to write.
063: *
064: * @throws IOException If there is an error writing the chunk.
065: */
066: public void write(long chunk) throws IOException {
067: long bitToWrite;
068: for (int i = (bitsInChunk - 1); i >= 0; i--) {
069: bitToWrite = (chunk >> i) & 0x1;
070: bitToWrite <<= positionInCurrentByte;
071: currentByte |= bitToWrite;
072: positionInCurrentByte--;
073: if (positionInCurrentByte < 0) {
074: out.write(currentByte);
075: currentByte = 0;
076: positionInCurrentByte = 7;
077: }
078: }
079: }
080:
081: /**
082: * This will close the stream.
083: *
084: * @throws IOException if there is an error closing the stream.
085: */
086: public void close() throws IOException {
087: if (positionInCurrentByte < 7) {
088: out.write(currentByte);
089: }
090: }
091:
092: /** Getter for property bitsToRead.
093: * @return Value of property bitsToRead.
094: */
095: public int getBitsInChunk() {
096: return bitsInChunk;
097: }
098:
099: /** Setter for property bitsToRead.
100: * @param bitsInChunkValue New value of property bitsToRead.
101: */
102: public void setBitsInChunk(int bitsInChunkValue) {
103: bitsInChunk = bitsInChunkValue;
104: }
105:
106: }
|