KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > 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 org.pdfbox.io;
32
33 import java.io.FilterInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 /**
38  * This class represents an ASCII85 stream.
39  *
40  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
41  * @version $Revision: 1.6 $
42  */

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

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

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

181     public final int read(byte[] data, int offset, int len) throws IOException JavaDoc
182     {
183         if(eof && index>=n)
184         {
185             return -1;
186         }
187         for(int i=0;i<len;i++)
188         {
189             if(index<n)
190             {
191                 data[i+offset]=b[index++];
192             }
193             else
194             {
195                 int t = read();
196                 if ( t == -1 )
197                 {
198                     return i;
199                 }
200                 data[i+offset]=(byte)t;
201             }
202         }
203         return len;
204     }
205
206     /**
207      * This will close the underlying stream and release any resources.
208      *
209      * @throws IOException If there is an error closing the underlying stream.
210      */

211     public void close() throws IOException JavaDoc
212     {
213         ascii = null;
214         eof = true;
215         b = null;
216         super.close();
217     }
218
219     /**
220      * non supported interface methods.
221      *
222      * @return False always.
223      */

224     public boolean markSupported()
225     {
226         return false;
227     }
228
229     /**
230      * Unsupported.
231      *
232      * @param nValue ignored.
233      *
234      * @return Always zero.
235      */

236     public long skip(long nValue)
237     {
238         return 0;
239     }
240
241     /**
242      * Unsupported.
243      *
244      * @return Always zero.
245      */

246     public int available()
247     {
248         return 0;
249     }
250
251     /**
252      * Unsupported.
253      *
254      * @param readlimit ignored.
255      */

256     public void mark(int readlimit)
257     {
258     }
259
260     /**
261      * Unsupported.
262      *
263      * @throws IOException telling that this is an unsupported action.
264      */

265     public void reset() throws IOException JavaDoc
266     {
267         throw new IOException JavaDoc("Reset is not supported");
268     }
269 }
Popular Tags