1 31 package org.pdfbox.filter; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 37 import org.pdfbox.cos.COSDictionary; 38 39 import org.pdfbox.persistence.util.COSHEXTable; 40 41 47 public class ASCIIHexFilter implements Filter 48 { 49 50 59 public void decode( InputStream compressedData, OutputStream result, COSDictionary options ) throws IOException 60 { 61 int value =0; 62 int firstByte = 0; 63 int secondByte = 0; 64 while( (firstByte = compressedData.read()) != -1 ) 65 { 66 value = REVERSE_HEX[firstByte] * 16; 67 secondByte = compressedData.read(); 68 if( secondByte >= 0 ) 69 { 70 value += REVERSE_HEX[ secondByte ]; 71 } 72 result.write( value ); 73 } 74 result.flush(); 75 } 76 77 private static final int[] REVERSE_HEX = 78 { 79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, }; 183 184 193 public void encode( InputStream rawData, OutputStream result, COSDictionary options ) throws IOException 194 { 195 int byteRead = 0; 196 while( (byteRead = rawData.read()) != -1 ) 197 { 198 int value = (byteRead+256)%256; 199 result.write( COSHEXTable.TABLE[value] ); 200 } 201 result.flush(); 202 } 203 } | Popular Tags |