1 31 package org.pdfbox.io; 32 33 import java.io.FilterInputStream ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 37 43 public class ASCII85InputStream extends FilterInputStream 44 { 45 private int index; 46 private int n; 47 private boolean eof; 48 49 private byte[] ascii; 50 private byte[] b; 51 52 57 public ASCII85InputStream( InputStream 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 74 public final int read() throws IOException 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; 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 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 ("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 181 public final int read(byte[] data, int offset, int len) throws IOException 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 211 public void close() throws IOException 212 { 213 ascii = null; 214 eof = true; 215 b = null; 216 super.close(); 217 } 218 219 224 public boolean markSupported() 225 { 226 return false; 227 } 228 229 236 public long skip(long nValue) 237 { 238 return 0; 239 } 240 241 246 public int available() 247 { 248 return 0; 249 } 250 251 256 public void mark(int readlimit) 257 { 258 } 259 260 265 public void reset() throws IOException 266 { 267 throw new IOException ("Reset is not supported"); 268 } 269 } | Popular Tags |