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.InputStream;
033: import java.io.IOException;
034:
035: /**
036: * This is an n-bit input stream. This means that you can read chunks of data
037: * as any number of bits, not just 8 bits like the regular input stream. Just set the
038: * number of bits that you would like to read on each call. The default is 8.
039: *
040: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
041: * @version $Revision: 1.4 $
042: */
043: public class NBitInputStream {
044: private int bitsInChunk;
045: private InputStream in;
046:
047: private int currentByte;
048: private int bitsLeftInCurrentByte;
049:
050: /**
051: * Constructor.
052: *
053: * @param is The input stream to read from.
054: */
055: public NBitInputStream(InputStream is) {
056: in = is;
057: bitsLeftInCurrentByte = 0;
058: bitsInChunk = 8;
059: }
060:
061: /**
062: * This will unread some data.
063: *
064: * @param data The data to put back into the stream.
065: */
066: public void unread(long data) {
067: data <<= bitsLeftInCurrentByte;
068: currentByte |= data;
069: bitsLeftInCurrentByte += bitsInChunk;
070: }
071:
072: /**
073: * This will read the next n bits from the stream and return the unsigned
074: * value of those bits.
075: *
076: * @return The next n bits from the stream.
077: *
078: * @throws IOException If there is an error reading from the underlying stream.
079: */
080: public long read() throws IOException {
081: long retval = 0;
082: for (int i = 0; i < bitsInChunk && retval != -1; i++) {
083: if (bitsLeftInCurrentByte == 0) {
084: currentByte = in.read();
085: bitsLeftInCurrentByte = 8;
086: }
087: if (currentByte == -1) {
088: retval = -1;
089: } else {
090: retval <<= 1;
091: retval |= ((currentByte >> (bitsLeftInCurrentByte - 1)) & 0x1);
092: bitsLeftInCurrentByte--;
093: }
094: }
095: return retval;
096: }
097:
098: /** Getter for property bitsToRead.
099: * @return Value of property bitsToRead.
100: */
101: public int getBitsInChunk() {
102: return bitsInChunk;
103: }
104:
105: /** Setter for property bitsToRead.
106: * @param bitsInChunkValue New value of property bitsToRead.
107: */
108: public void setBitsInChunk(int bitsInChunkValue) {
109: bitsInChunk = bitsInChunkValue;
110: }
111:
112: }
|