1 31 package org.pdfbox.io; 32 33 import java.io.OutputStream ; 34 import java.io.IOException ; 35 36 42 public class NBitOutputStream 43 { 44 private int bitsInChunk; 45 private OutputStream out; 46 47 private int currentByte; 48 private int positionInCurrentByte; 49 50 55 public NBitOutputStream( OutputStream os ) 56 { 57 out = os; 58 currentByte = 0; 59 positionInCurrentByte = 7; 60 } 61 62 69 public void write( long chunk ) throws IOException 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 92 public void close() throws IOException 93 { 94 if( positionInCurrentByte < 7 ) 95 { 96 out.write( currentByte ); 97 } 98 } 99 100 103 public int getBitsInChunk() 104 { 105 return bitsInChunk; 106 } 107 108 111 public void setBitsInChunk(int bitsInChunkValue) 112 { 113 bitsInChunk = bitsInChunkValue; 114 } 115 116 } | Popular Tags |