KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > io > ASCII85InputStream


1 /*
2  * Copyright (c) 2003, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package com.imagero.uio.io;
32
33 import java.io.FilterInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36
37 /**
38  * This class represents an ASCII85 stream.
39  *
40  * @author Ben Litchfield
41  * @version $Revision: 1.2 $
42  */

43 public class ASCII85InputStream extends FilterInputStream JavaDoc {
44     private int index;
45     private int n;
46     private boolean eof;
47
48     private byte[] ascii;
49     private byte[] b;
50
51     /**
52      * Constructor.
53      *
54      * @param is The input stream to actually read from.
55      */

56     public ASCII85InputStream(InputStream JavaDoc is) {
57         super(is);
58         ascii = new byte[5];
59         b = new byte[4];
60     }
61
62     /**
63      * This will read the next byte from the stream.
64      *
65      * @return The next byte read from the stream.
66      *
67      * @throws IOException If there is an error reading from the wrapped stream.
68      */

69     public final int read() throws IOException JavaDoc {
70         if (index >= n) {
71             if (eof) {
72                 return -1;
73             }
74             index = 0;
75             int k;
76             byte z;
77             do {
78                 int zz = (byte) in.read();
79                 if (zz == -1) {
80                     eof = true;
81                     return -1;
82                 }
83                 z = (byte) zz;
84             }
85             while (z == '\n' || z == '\r' || z == ' ');
86
87             if (z == '~' || z == 'x') {
88                 eof = true;
89                 ascii = b = null;
90                 n = 0;
91                 return -1;
92             }
93             else if (z == 'z') {
94                 b[0] = b[1] = b[2] = b[3] = 0;
95                 n = 4;
96             }
97             else {
98                 ascii[0] = z; // may be EOF here....
99
for (k = 1; k < 5; ++k) {
100                     do {
101                         int zz = (byte) in.read();
102                         if (zz == -1) {
103                             eof = true;
104                             return -1;
105                         }
106                         z = (byte) zz;
107                     }
108                     while (z == '\n' || z == '\r' || z == ' ');
109                     ascii[k] = z;
110                     if (z == '~' || z == 'x') {
111                         break;
112                     }
113                 }
114                 n = k - 1;
115                 if (n == 0) {
116                     eof = true;
117                     ascii = null;
118                     b = null;
119                     return -1;
120                 }
121                 if (k < 5) {
122                     for (++k; k < 5; ++k) {
123                         ascii[k] = 0x21;
124                     }
125                     eof = true;
126                 }
127                 // decode stream
128
long t = 0;
129                 for (k = 0; k < 5; ++k) {
130                     z = (byte) (ascii[k] - 0x21);
131                     if (z < 0 || z > 93) {
132                         n = 0;
133                         eof = true;
134                         ascii = null;
135                         b = null;
136                         throw new IOException JavaDoc("Invalid data in Ascii85 stream");
137                     }
138                     t = (t * 85L) + z;
139                 }
140                 for (k = 3; k >= 0; --k) {
141                     b[k] = (byte) (t & 0xFFL);
142                     t >>>= 8;
143                 }
144             }
145         }
146         return b[index++] & 0xFF;
147     }
148
149     /**
150      * This will read a chunk of data.
151      *
152      * @param data The buffer to write data to.
153      * @param offset The offset into the data stream.
154      * @param len The number of byte to attempt to read.
155      *
156      * @return The number of bytes actually read.
157      *
158      * @throws IOException If there is an error reading data from the underlying stream.
159      */

160     public final int read(byte[] data, int offset, int len) throws IOException JavaDoc {
161         if (eof && index >= n) {
162             return -1;
163         }
164         for (int i = 0; i < len; i++) {
165             if (index < n) {
166                 data[i + offset] = b[index++];
167             }
168             else {
169                 int t = read();
170                 if (t == -1) {
171                     return i;
172                 }
173                 data[i + offset] = (byte) t;
174             }
175         }
176         return len;
177     }
178
179     /**
180      * This will close the underlying stream and release any resources.
181      *
182      * @throws IOException If there is an error closing the underlying stream.
183      */

184     public void close() throws IOException JavaDoc {
185         ascii = null;
186         eof = true;
187         b = null;
188         super.close();
189     }
190
191     /**
192      * non supported interface methods.
193      *
194      * @return False always.
195      */

196     public boolean markSupported() {
197         return false;
198     }
199
200     /**
201      * Unsupported.
202      *
203      * @param nValue ignored.
204      *
205      * @return Always zero.
206      */

207     public long skip(long nValue) {
208         return 0;
209     }
210
211     /**
212      * Unsupported.
213      *
214      * @return Always zero.
215      */

216     public int available() {
217         if (eof) {
218             return 0;
219         }
220         else {
221             return 1;
222         }
223     }
224
225     /**
226      * Unsupported.
227      *
228      * @param readlimit ignored.
229      */

230     public void mark(int readlimit) {
231     }
232
233     /**
234      * Unsupported.
235      *
236      * @throws IOException telling that this is an unsupported action.
237      */

238     public void reset() throws IOException JavaDoc {
239         throw new IOException JavaDoc("Reset is not supported");
240     }
241 }
242
Popular Tags