1 31 package org.pdfbox.io; 32 33 import java.io.FilterOutputStream ; 34 import java.io.IOException ; 35 import java.io.OutputStream ; 36 37 43 public class ASCII85OutputStream extends FilterOutputStream 44 { 45 46 private int lineBreak; 47 private int count; 48 49 private byte[] indata; 50 private byte[] outdata; 51 52 56 private int maxline; 57 private boolean flushed; 58 private char terminator; 59 60 65 public ASCII85OutputStream( OutputStream out ) 66 { 67 super( out ); 68 lineBreak = 36*2; 69 maxline = 36*2; 70 count=0; 71 indata=new byte[4]; 72 outdata=new byte[5]; 73 flushed=true; 74 terminator='~'; 75 } 76 77 82 public void setTerminator(char term) 83 { 84 if(term<118 || term>126 || term=='z') 85 { 86 throw new IllegalArgumentException ("Terminator must be 118-126 excluding z"); 87 } 88 terminator=term; 89 } 90 91 96 public char getTerminator() 97 { 98 return terminator; 99 } 100 101 106 public void setLineLength(int l) 107 { 108 if( lineBreak > l ) 109 { 110 lineBreak = l; 111 } 112 maxline=l; 113 } 114 115 120 public int getLineLength() 121 { 122 return maxline; 123 } 124 125 128 private final void transformASCII85() 129 { 130 long word; 131 word=( (((indata[0] << 8) | (indata[1] &0xFF)) << 16) | 132 ( (indata[2] & 0xFF) << 8) | (indata[3] & 0xFF) 133 ) & 0xFFFFFFFFL; 134 136 if (word == 0 ) 137 { 138 outdata[0]=(byte)'z'; 139 outdata[1]=0; 140 return; 141 } 142 long x; 143 x=word/(85L*85L*85L*85L); 144 outdata[0]=(byte)(x+'!'); 146 word-=x*85L*85L*85L*85L; 147 148 x=word/(85L*85L*85L); 149 outdata[1]=(byte)(x+'!'); 151 word-=x*85L*85L*85L; 152 153 x=word/(85L*85L); 154 outdata[2]=(byte)(x+'!'); 156 word-=x*85L*85L; 157 158 x=word/85L; 159 outdata[3]=(byte)(x+'!'); 161 162 164 outdata[4]=(byte)((word%85L)+'!'); 166 } 167 168 175 public final void write(int b) throws IOException 176 { 177 flushed=false; 178 indata[count++]=(byte)b; 179 if(count < 4 ) 180 { 181 return; 182 } 183 transformASCII85(); 184 for(int i=0;i<5;i++) 185 { 186 if(outdata[i]==0) 187 { 188 break; 189 } 190 out.write(outdata[i]); 191 if(--lineBreak==0) 192 { 193 out.write('\n'); 194 lineBreak=maxline; 195 } 196 } 197 count = 0; 198 } 199 200 209 public final void write(byte[] b,int off, int sz) throws IOException 210 { 211 for(int i=0;i<sz;i++) 212 { 213 if(count < 3) 214 { 215 indata[count++]=b[off+i]; 216 } 217 else 218 { 219 write(b[off+i]); 220 } 221 } 222 } 223 224 229 public final void flush() throws IOException 230 { 231 if(flushed) 232 { 233 return; 234 } 235 if(count > 0 ) 236 { 237 for( int i=count; i<4; i++ ) 238 { 239 indata[i]=0; 240 } 241 transformASCII85(); 242 if(outdata[0]=='z') 243 { 244 for(int i=0;i<5;i++) { 246 outdata[i]=(byte)'!'; 247 } 248 } 249 for(int i=0;i<count+1;i++) 250 { 251 out.write(outdata[i]); 252 if(--lineBreak==0) 253 { 254 out.write('\n'); 255 lineBreak=maxline; 256 } 257 } 258 } 259 if(--lineBreak==0) 260 { 261 out.write('\n'); 262 } 263 out.write(terminator); 264 out.write('\n'); 265 count = 0; 266 lineBreak=maxline; 267 flushed=true; 268 super.flush(); 269 } 270 271 276 public void close() throws IOException 277 { 278 try 279 { 280 super.close(); 281 } 282 finally 283 { 284 indata=outdata=null; 285 } 286 } 287 288 293 protected void finalize() throws Throwable 294 { 295 try 296 { 297 flush(); 298 } 299 finally 300 { 301 super.finalize(); 302 } 303 } 304 } | Popular Tags |