1 31 package org.pdfbox.io; 32 33 import java.io.InputStream ; 34 import java.io.IOException ; 35 36 44 public class NBitInputStream 45 { 46 private int bitsInChunk; 47 private InputStream in; 48 49 private int currentByte; 50 private int bitsLeftInCurrentByte; 51 52 57 public NBitInputStream( InputStream is ) 58 { 59 in = is; 60 bitsLeftInCurrentByte = 0; 61 bitsInChunk = 8; 62 } 63 64 69 public void unread( long data ) 70 { 71 data <<= bitsLeftInCurrentByte; 72 currentByte |= data; 73 bitsLeftInCurrentByte += bitsInChunk; 74 } 75 76 84 public long read() throws IOException 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 111 public int getBitsInChunk() 112 { 113 return bitsInChunk; 114 } 115 116 119 public void setBitsInChunk(int bitsInChunkValue) 120 { 121 bitsInChunk = bitsInChunkValue; 122 } 123 124 } | Popular Tags |