1 31 package org.pdfbox.pdmodel.common; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.ByteArrayOutputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 import org.pdfbox.cos.COSArray; 45 import org.pdfbox.cos.COSBase; 46 import org.pdfbox.cos.COSDictionary; 47 import org.pdfbox.cos.COSName; 48 import org.pdfbox.cos.COSStream; 49 50 import org.pdfbox.filter.Filter; 51 import org.pdfbox.filter.FilterManager; 52 53 import org.pdfbox.pdmodel.PDDocument; 54 55 import org.pdfbox.pdmodel.common.filespecification.PDFileSpecification; 56 57 64 public class PDStream implements COSObjectable 65 { 66 private COSStream stream; 67 68 71 protected PDStream() 72 { 73 } 75 76 81 public PDStream( PDDocument document ) 82 { 83 stream = new COSStream( document.getDocument().getScratchFile() ); 84 } 85 86 91 public PDStream( COSStream str ) 92 { 93 stream = str; 94 } 95 96 104 public PDStream( PDDocument doc, InputStream str ) throws IOException 105 { 106 this( doc, str, false ); 107 } 108 109 118 public PDStream( PDDocument doc, InputStream str, boolean filtered ) throws IOException 119 { 120 OutputStream output = null; 121 try 122 { 123 stream = new COSStream( doc.getDocument().getScratchFile() ); 124 if( filtered ) 125 { 126 output = stream.createFilteredStream(); 127 } 128 else 129 { 130 output = stream.createUnfilteredStream(); 131 } 132 byte[] buffer = new byte[ 1024 ]; 133 int amountRead = -1; 134 while( (amountRead = str.read(buffer)) != -1 ) 135 { 136 output.write( buffer, 0, amountRead ); 137 } 138 } 139 finally 140 { 141 if( output != null ) 142 { 143 output.close(); 144 } 145 if( str != null ) 146 { 147 str.close(); 148 } 149 } 150 } 151 152 156 public void addCompression() 157 { 158 List filters = getFilters(); 159 if( filters == null ) 160 { 161 filters = new ArrayList (); 162 filters.add( COSName.FLATE_DECODE ); 163 setFilters( filters ); 164 } 165 } 166 167 173 public static PDStream createFromCOS( COSBase base ) throws IOException 174 { 175 PDStream retval = null; 176 if( base instanceof COSStream ) 177 { 178 retval = new PDStream( (COSStream)base ); 179 } 180 else if( base instanceof COSArray ) 181 { 182 retval = new PDStream( new COSStreamArray( (COSArray)base ) ); 183 } 184 else 185 { 186 if( base != null ) 187 { 188 throw new IOException ( "Contents are unknown type:" + base.getClass().getName() ); 189 } 190 } 191 return retval; 192 } 193 194 195 200 public COSBase getCOSObject() 201 { 202 return stream; 203 } 204 205 212 public OutputStream createOutputStream() throws IOException 213 { 214 return stream.createUnfilteredStream(); 215 } 216 217 224 public InputStream createInputStream() throws IOException 225 { 226 return stream.getUnfilteredStream(); 227 } 228 229 237 public InputStream getPartiallyFilteredStream( List stopFilters ) throws IOException 238 { 239 FilterManager manager = stream.getFilterManager(); 240 InputStream is = stream.getFilteredStream(); 241 ByteArrayOutputStream os = new ByteArrayOutputStream (); 242 List filters = getFilters(); 243 Iterator iter = filters.iterator(); 244 String nextFilter = null; 245 boolean done = false; 246 while( iter.hasNext() && !done ) 247 { 248 os.reset(); 249 nextFilter = (String )iter.next(); 250 if( stopFilters.contains( nextFilter ) ) 251 { 252 done = true; 253 } 254 else 255 { 256 Filter filter = manager.getFilter( COSName.getPDFName(nextFilter) ); 257 filter.decode( is, os, stream ); 258 is = new ByteArrayInputStream ( os.toByteArray() ); 259 } 260 } 261 return is; 262 } 263 264 269 public COSStream getStream() 270 { 271 return stream; 272 } 273 274 280 public int getLength() 281 { 282 return stream.getInt( "Length", 0 ); 283 } 284 285 290 public List getFilters() 291 { 292 List retval = null; 293 COSBase filters = stream.getFilters(); 294 if( filters instanceof COSName ) 295 { 296 COSName name = (COSName)filters; 297 retval = new COSArrayList( name.getName(), name, stream, "Filter" ); 298 } 299 else if( filters instanceof COSArray ) 300 { 301 retval = COSArrayList.convertCOSNameCOSArrayToList( (COSArray)filters ); 302 } 303 return retval; 304 } 305 306 311 public void setFilters( List filters ) 312 { 313 COSBase obj = COSArrayList.convertStringListToCOSNameCOSArray( filters ); 314 stream.setItem( "Filter", obj ); 315 } 316 317 325 public List getDecodeParams() throws IOException 326 { 327 List retval = null; 328 329 COSBase dp = stream.getDictionaryObject( "DecodeParms" ); 330 if( dp == null ) 331 { 332 dp = stream.getDictionaryObject( "DP" ); 334 } 335 if( dp instanceof COSDictionary ) 336 { 337 Map map = COSDictionaryMap.convertBasicTypesToMap( (COSDictionary)dp ); 338 retval = new COSArrayList(map, dp, stream, "DecodeParams" ); 339 } 340 else if( dp instanceof COSArray ) 341 { 342 COSArray array = (COSArray)dp; 343 List actuals = new ArrayList (); 344 for( int i=0; i<array.size(); i++ ) 345 { 346 actuals.add( 347 COSDictionaryMap.convertBasicTypesToMap( 348 (COSDictionary)array.getObject( i ) ) ); 349 } 350 retval = new COSArrayList(actuals, array); 351 } 352 353 return retval; 354 } 355 356 361 public void setDecodeParams( List decodeParams ) 362 { 363 stream.setItem( 364 "DecodeParams", COSArrayList.converterToCOSArray( decodeParams ) ); 365 } 366 367 375 public PDFileSpecification getFile() throws IOException 376 { 377 COSBase f = stream.getDictionaryObject( "F" ); 378 PDFileSpecification retval = PDFileSpecification.createFS( f ); 379 return retval; 380 } 381 382 386 public void setFile( PDFileSpecification f ) 387 { 388 stream.setItem( "F", f ); 389 } 390 391 396 public List getFileFilters() 397 { 398 List retval = null; 399 COSBase filters = stream.getDictionaryObject( "FFilter" ); 400 if( filters instanceof COSName ) 401 { 402 COSName name = (COSName)filters; 403 retval = new COSArrayList( name.getName(), name, stream, "FFilter" ); 404 } 405 else if( filters instanceof COSArray ) 406 { 407 retval = COSArrayList.convertCOSNameCOSArrayToList( (COSArray)filters ); 408 } 409 return retval; 410 } 411 412 417 public void setFileFilters( List filters ) 418 { 419 COSBase obj = COSArrayList.convertStringListToCOSNameCOSArray( filters ); 420 stream.setItem( "FFilter", obj ); 421 } 422 423 431 public List getFileDecodeParams() throws IOException 432 { 433 List retval = null; 434 435 COSBase dp = stream.getDictionaryObject( "FDecodeParms" ); 436 if( dp instanceof COSDictionary ) 437 { 438 Map map = COSDictionaryMap.convertBasicTypesToMap( (COSDictionary)dp ); 439 retval = new COSArrayList(map, dp, stream, "FDecodeParams" ); 440 } 441 else if( dp instanceof COSArray ) 442 { 443 COSArray array = (COSArray)dp; 444 List actuals = new ArrayList (); 445 for( int i=0; i<array.size(); i++ ) 446 { 447 actuals.add( 448 COSDictionaryMap.convertBasicTypesToMap( 449 (COSDictionary)array.getObject( i ) ) ); 450 } 451 retval = new COSArrayList(actuals, array); 452 } 453 454 return retval; 455 } 456 457 462 public void setFileDecodeParams( List decodeParams ) 463 { 464 stream.setItem( 465 "FDecodeParams", COSArrayList.converterToCOSArray( decodeParams ) ); 466 } 467 468 474 public byte[] getByteArray() throws IOException 475 { 476 ByteArrayOutputStream output = new ByteArrayOutputStream (); 477 byte[] buf = new byte[1024]; 478 InputStream is = null; 479 try 480 { 481 is = createInputStream(); 482 int amountRead = -1; 483 while( (amountRead = is.read( buf )) != -1) 484 { 485 output.write( buf, 0, amountRead ); 486 } 487 } 488 finally 489 { 490 if( is != null ) 491 { 492 is.close(); 493 } 494 } 495 return output.toByteArray(); 496 } 497 498 508 public String getInputStreamAsString() throws IOException 509 { 510 byte[] bStream = getByteArray(); 511 return new String (bStream); 512 } 513 514 520 public PDMetadata getMetadata() 521 { 522 PDMetadata retval = null; 523 COSStream mdStream = (COSStream)stream.getDictionaryObject( "Metadata" ); 524 if( mdStream != null ) 525 { 526 retval = new PDMetadata( mdStream ); 527 } 528 return retval; 529 } 530 531 536 public void setMetadata( PDMetadata meta ) 537 { 538 stream.setItem( "Metadata", meta ); 539 } 540 } | Popular Tags |