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.filter;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.OutputStream;
035:
036: import org.pdfbox.cos.COSDictionary;
037:
038: import org.pdfbox.persistence.util.COSHEXTable;
039:
040: /**
041: * This is the used for the ASCIIHexDecode filter.
042: *
043: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
044: * @version $Revision: 1.8 $
045: */
046: public class ASCIIHexFilter implements Filter {
047:
048: /**
049: * This will decode some compressed data.
050: *
051: * @param compressedData The compressed byte stream.
052: * @param result The place to write the uncompressed byte stream.
053: * @param options The options to use to encode the data.
054: *
055: * @throws IOException If there is an error decompressing the stream.
056: */
057: public void decode(InputStream compressedData, OutputStream result,
058: COSDictionary options) throws IOException {
059: int value = 0;
060: int firstByte = 0;
061: int secondByte = 0;
062: while ((firstByte = compressedData.read()) != -1) {
063: value = REVERSE_HEX[firstByte] * 16;
064: secondByte = compressedData.read();
065: if (secondByte >= 0) {
066: value += REVERSE_HEX[secondByte];
067: }
068: result.write(value);
069: }
070: result.flush();
071: }
072:
073: private static final int[] REVERSE_HEX = { -1, //0
074: -1, //1
075: -1, //2
076: -1, //3
077: -1, //4
078: -1, //5
079: -1, //6
080: -1, //7
081: -1, //8
082: -1, //9
083: -1, //10
084: -1, //11
085: -1, //12
086: -1, //13
087: -1, //14
088: -1, //15
089: -1, //16
090: -1, //17
091: -1, //18
092: -1, //19
093: -1, //20
094: -1, //21
095: -1, //22
096: -1, //23
097: -1, //24
098: -1, //25
099: -1, //26
100: -1, //27
101: -1, //28
102: -1, //29
103: -1, //30
104: -1, //31
105: -1, //32
106: -1, //33
107: -1, //34
108: -1, //35
109: -1, //36
110: -1, //37
111: -1, //38
112: -1, //39
113: -1, //40
114: -1, //41
115: -1, //42
116: -1, //43
117: -1, //44
118: -1, //45
119: -1, //46
120: -1, //47
121: 0, //48
122: 1, //49
123: 2, //50
124: 3, //51
125: 4, //52
126: 5, //53
127: 6, //54
128: 7, //55
129: 8, //56
130: 9, //57
131: -1, //58
132: -1, //59
133: -1, //60
134: -1, //61
135: -1, //62
136: -1, //63
137: -1, //64
138: 10, //65
139: 11, //66
140: 12, //67
141: 13, //68
142: 14, //69
143: 15, //70
144: -1, //71
145: -1, //72
146: -1, //73
147: -1, //74
148: -1, //75
149: -1, //76
150: -1, //77
151: -1, //78
152: -1, //79
153: -1, //80
154: -1, //81
155: -1, //82
156: -1, //83
157: -1, //84
158: -1, //85
159: -1, //86
160: -1, //87
161: -1, //88
162: -1, //89
163: -1, //90
164: -1, //91
165: -1, //92
166: -1, //93
167: -1, //94
168: -1, //95
169: -1, //96
170: 10, //97
171: 11, //98
172: 12, //99
173: 13, //100
174: 14, //101
175: 15, //102
176: };
177:
178: /**
179: * This will encode some data.
180: *
181: * @param rawData The raw data to encode.
182: * @param result The place to write to encoded results to.
183: * @param options The options to use to encode the data.
184: *
185: * @throws IOException If there is an error compressing the stream.
186: */
187: public void encode(InputStream rawData, OutputStream result,
188: COSDictionary options) throws IOException {
189: int byteRead = 0;
190: while ((byteRead = rawData.read()) != -1) {
191: int value = (byteRead + 256) % 256;
192: result.write(COSHEXTable.TABLE[value]);
193: }
194: result.flush();
195: }
196: }
|