1 31 package org.pdfbox.pdmodel.common; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 import java.io.SequenceInputStream ; 38 39 import java.util.ArrayList ; 40 import java.util.List ; 41 import java.util.Vector ; 42 43 import org.pdfbox.cos.COSArray; 44 import org.pdfbox.cos.COSBase; 45 import org.pdfbox.cos.COSDictionary; 46 import org.pdfbox.cos.COSName; 47 import org.pdfbox.cos.COSStream; 48 import org.pdfbox.cos.ICOSVisitor; 49 50 import org.pdfbox.exceptions.COSVisitorException; 51 import org.pdfbox.io.RandomAccess; 52 53 import org.pdfbox.pdfparser.PDFStreamParser; 54 55 61 public class COSStreamArray extends COSStream 62 { 63 private COSArray streams; 64 65 69 private COSStream firstStream; 70 71 76 public COSStreamArray( COSArray array ) 77 { 78 super( new COSDictionary(), null ); 79 streams = array; 80 if( array.size() > 0 ) 81 { 82 firstStream = (COSStream)array.getObject( 0 ); 83 } 84 } 85 90 public RandomAccess getScratchFile() 91 { 92 return firstStream.getScratchFile(); 93 } 94 95 102 public COSBase getItem( COSName key ) 103 { 104 return firstStream.getItem( key ); 105 } 106 107 115 public COSBase getDictionaryObject( COSName key ) 116 { 117 return firstStream.getDictionaryObject( key ); 118 } 119 120 123 public String toString() 124 { 125 String result = "COSStream{}"; 126 return result; 127 } 128 129 136 public List getStreamTokens() throws IOException 137 { 138 List retval = null; 139 if( streams.size() > 0 ) 140 { 141 PDFStreamParser parser = new PDFStreamParser( this ); 142 parser.parse(); 143 retval = parser.getTokens(); 144 } 145 else 146 { 147 retval = new ArrayList (); 148 } 149 return retval; 150 } 151 152 157 public COSDictionary getDictionary() 158 { 159 return firstStream; 160 } 161 162 169 public InputStream getFilteredStream() throws IOException 170 { 171 throw new IOException ( "Error: Not allowed to get filtered stream from array of streams." ); 172 183 } 184 185 192 public InputStream getUnfilteredStream() throws IOException 193 { 194 Vector inputStreams = new Vector (); 195 byte[] inbetweenStreamBytes = "\n".getBytes(); 196 197 for( int i=0;i<streams.size(); i++ ) 198 { 199 COSStream stream = (COSStream)streams.getObject( i ); 200 inputStreams.add( stream.getUnfilteredStream() ); 201 inputStreams.add( new ByteArrayInputStream ( inbetweenStreamBytes ) ); 206 } 207 208 return new SequenceInputStream ( inputStreams.elements() ); 209 } 210 211 218 public Object accept(ICOSVisitor visitor) throws COSVisitorException 219 { 220 return streams.accept( visitor ); 221 } 222 223 224 233 public COSBase getFilters() 234 { 235 return firstStream.getFilters(); 236 } 237 238 247 public OutputStream createFilteredStream() throws IOException 248 { 249 return firstStream.createFilteredStream(); 250 } 251 252 263 public OutputStream createFilteredStream( COSBase expectedLength ) throws IOException 264 { 265 return firstStream.createFilteredStream( expectedLength ); 266 } 267 268 275 public void setFilters(COSBase filters) throws IOException 276 { 277 firstStream.setFilters( filters ); 280 } 281 282 289 public OutputStream createUnfilteredStream() throws IOException 290 { 291 return firstStream.createUnfilteredStream(); 292 } 293 294 299 public void appendStream(COSStream streamToAppend) 300 { 301 streams.add(streamToAppend); 302 } 303 304 } | Popular Tags |