KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > COSStreamArray


1 /**
2  * Copyright (c) 2003-2006, 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.pdmodel.common;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.SequenceInputStream JavaDoc;
38
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Vector JavaDoc;
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 /**
56  * This will take an array of streams and sequence them together.
57  *
58  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
59  * @version $Revision: 1.9 $
60  */

61 public class COSStreamArray extends COSStream
62 {
63     private COSArray streams;
64
65     /**
66      * The first stream will be used to delegate some of the methods for this
67      * class.
68      */

69     private COSStream firstStream;
70
71     /**
72      * Constructor.
73      *
74      * @param array The array of COSStreams to concatenate together.
75      */

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     /**
86      * This will get the scratch file associated with this stream.
87      *
88      * @return The scratch file where this stream is being stored.
89      */

90     public RandomAccess getScratchFile()
91     {
92         return firstStream.getScratchFile();
93     }
94
95     /**
96      * This will get an object from this streams dictionary.
97      *
98      * @param key The key to the object.
99      *
100      * @return The dictionary object with the key or null if one does not exist.
101      */

102     public COSBase getItem( COSName key )
103     {
104         return firstStream.getItem( key );
105     }
106
107    /**
108      * This will get an object from this streams dictionary and dereference it
109      * if necessary.
110      *
111      * @param key The key to the object.
112      *
113      * @return The dictionary object with the key or null if one does not exist.
114      */

115     public COSBase getDictionaryObject( COSName key )
116     {
117         return firstStream.getDictionaryObject( key );
118     }
119
120     /**
121      * {@inheritDoc}
122      */

123     public String JavaDoc toString()
124     {
125         String JavaDoc result = "COSStream{}";
126         return result;
127     }
128
129     /**
130      * This will get all the tokens in the stream.
131      *
132      * @return All of the tokens in the stream.
133      *
134      * @throws IOException If there is an error parsing the stream.
135      */

136     public List JavaDoc getStreamTokens() throws IOException JavaDoc
137     {
138         List JavaDoc 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 JavaDoc();
148         }
149         return retval;
150     }
151
152     /**
153      * This will get the dictionary that is associated with this stream.
154      *
155      * @return the object that is associated with this stream.
156      */

157     public COSDictionary getDictionary()
158     {
159         return firstStream;
160     }
161
162     /**
163      * This will get the stream with all of the filters applied.
164      *
165      * @return the bytes of the physical (endoced) stream
166      *
167      * @throws IOException when encoding/decoding causes an exception
168      */

169     public InputStream JavaDoc getFilteredStream() throws IOException JavaDoc
170     {
171         throw new IOException JavaDoc( "Error: Not allowed to get filtered stream from array of streams." );
172         /**
173         Vector inputStreams = new Vector();
174         byte[] inbetweenStreamBytes = "\n".getBytes();
175
176         for( int i=0;i<streams.size(); i++ )
177         {
178             COSStream stream = (COSStream)streams.getObject( i );
179         }
180
181         return new SequenceInputStream( inputStreams.elements() );
182         **/

183     }
184
185     /**
186      * This will get the logical content stream with none of the filters.
187      *
188      * @return the bytes of the logical (decoded) stream
189      *
190      * @throws IOException when encoding/decoding causes an exception
191      */

192     public InputStream JavaDoc getUnfilteredStream() throws IOException JavaDoc
193     {
194         Vector JavaDoc inputStreams = new Vector JavaDoc();
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             //handle the case where there is no whitespace in the
202
//between streams in the contents array, without this
203
//it is possible that two operators will get concatenated
204
//together
205
inputStreams.add( new ByteArrayInputStream JavaDoc( inbetweenStreamBytes ) );
206         }
207
208         return new SequenceInputStream JavaDoc( inputStreams.elements() );
209     }
210
211     /**
212      * visitor pattern double dispatch method.
213      *
214      * @param visitor The object to notify when visiting this object.
215      * @return any object, depending on the visitor implementation, or null
216      * @throws COSVisitorException If an error occurs while visiting this object.
217      */

218     public Object JavaDoc accept(ICOSVisitor visitor) throws COSVisitorException
219     {
220         return streams.accept( visitor );
221     }
222
223
224     /**
225      * This will return the filters to apply to the byte stream
226      * the method will return.
227      * - null if no filters are to be applied
228      * - a COSName if one filter is to be applied
229      * - a COSArray containing COSNames if multiple filters are to be applied
230      *
231      * @return the COSBase object representing the filters
232      */

233     public COSBase getFilters()
234     {
235         return firstStream.getFilters();
236     }
237
238     /**
239      * This will create a new stream for which filtered byte should be
240      * written to. You probably don't want this but want to use the
241      * createUnfilteredStream, which is used to write raw bytes to.
242      *
243      * @return A stream that can be written to.
244      *
245      * @throws IOException If there is an error creating the stream.
246      */

247     public OutputStream JavaDoc createFilteredStream() throws IOException JavaDoc
248     {
249         return firstStream.createFilteredStream();
250     }
251
252     /**
253      * This will create a new stream for which filtered byte should be
254      * written to. You probably don't want this but want to use the
255      * createUnfilteredStream, which is used to write raw bytes to.
256      *
257      * @param expectedLength An entry where a length is expected.
258      *
259      * @return A stream that can be written to.
260      *
261      * @throws IOException If there is an error creating the stream.
262      */

263     public OutputStream JavaDoc createFilteredStream( COSBase expectedLength ) throws IOException JavaDoc
264     {
265         return firstStream.createFilteredStream( expectedLength );
266     }
267
268     /**
269      * set the filters to be applied to the stream.
270      *
271      * @param filters The filters to set on this stream.
272      *
273      * @throws IOException If there is an error clearing the old filters.
274      */

275     public void setFilters(COSBase filters) throws IOException JavaDoc
276     {
277         //should this be allowed? Should this
278
//propagate to all streams in the array?
279
firstStream.setFilters( filters );
280     }
281
282     /**
283      * This will create an output stream that can be written to.
284      *
285      * @return An output stream which raw data bytes should be written to.
286      *
287      * @throws IOException If there is an error creating the stream.
288      */

289     public OutputStream JavaDoc createUnfilteredStream() throws IOException JavaDoc
290     {
291         return firstStream.createUnfilteredStream();
292     }
293     
294     /**
295      * Appends a new stream to the array that represents this object's stream.
296      *
297      * @param streamToAppend The stream to append.
298      */

299     public void appendStream(COSStream streamToAppend)
300     {
301         streams.add(streamToAppend);
302     }
303
304 }
Popular Tags