KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > stream > PdfDecodeStream


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx.stream;
6
7 import java.io.*;
8 import java.util.*;
9 import com.etymon.pjx.*;
10
11 /**
12    Performs pipelined stream filtering to attempt to decode a stream.
13    This class is synchronized.
14    @author Nassib Nassar
15 */

16 public class PdfDecodeStream {
17
18         protected static final PdfName PDFNAME_FILTER = new PdfName("Filter");
19     protected static final PdfName PDFNAME_FLATEDECODE = new PdfName("FlateDecode");
20     
21     /**
22        The manager associated with the PDF document.
23     */

24     protected PdfManager _m;
25
26     /**
27        A flate filter instance to use for decoding.
28      */

29     protected PdfFlateFilter _flateFilter;
30     
31     /**
32        Constructs an instance of this class with a specified
33        manager.
34        @param manager the manager instance.
35      */

36     public PdfDecodeStream(PdfManager manager) {
37         _m = manager;
38         _flateFilter = new PdfFlateFilter(manager);
39     }
40
41     /**
42        Applies a sequence of stream filter decoders to the
43        specified stream, based on the stream dictionary's Filter
44        entry, in order to decode the stream. If the stream is
45        encoded with an unsupported filter, this method will throw
46        {@link PdfDecoderNotSupportedException
47        PdfDecoderNotSupportedException} to indicate that it is
48        unable to decode the stream. If the stream is not encoded
49        with any filters, this method returns the original stream
50        unmodified.
51        @param stream the stream to decode.
52        @return the decoded stream, or the original stream if it is
53        not encoded with any filters.
54        @throws IOException
55        @throws PdfFormatException
56        @throws PdfDecoderException
57      */

58     public PdfStream decode(PdfStream stream) throws IOException, PdfFormatException, PdfDecoderException {
59         synchronized (this) {
60             synchronized (_m) {
61                 
62                 PdfManager m = _m;
63                 
64                 // get the filter list
65
List filters = getFilterList(m, stream.getDictionary().getMap());
66                 if (filters == null) {
67                     return stream;
68                 }
69                 
70                 // cycle through for each and decode via the
71
// appropriate filter
72
PdfStream filtered = stream;
73                 for (Iterator t = filters.iterator(); t.hasNext(); ) {
74                     
75                     // get the first filter
76
Object JavaDoc obj = t.next();
77                     if ( !(obj instanceof PdfName) ) {
78                         throw new PdfFormatException("Stream filter is not a name object.");
79                     }
80                     PdfName filter = (PdfName)obj;
81                     
82                     if (filter.equals(_flateFilter.getName())) {
83                         filtered = _flateFilter.decode(filtered);
84                     } else {
85                         throw new PdfDecoderNotSupportedException(
86                             "Stream filter decoder \"" +
87                             filter.getString() + "\" not supported.");
88                     }
89                     
90                 }
91                 
92                 // return the resultant stream
93
return filtered;
94
95             }
96         }
97     }
98     
99     /**
100        Removes the first element of a filter list, adds the filter
101        list to a stream dictionary map, and returns the resultant
102        stream dictionary.
103        @param filters the filter list.
104        @param streamDict the stream dictionary map.
105        @throws PdfFormatException
106      */

107     protected static void modifyFilterList(List filters, Map streamDict) throws PdfFormatException {
108
109         // remove first element
110
filters.remove(0);
111
112         // add the filter list to the stream dictionary
113
if (filters.size() == 0) {
114             streamDict.remove(PDFNAME_FILTER);
115         } else {
116             streamDict.put(PDFNAME_FILTER, filters);
117         }
118
119     }
120
121     /**
122        Extracts the filter list from a stream dictionary map.
123        @param manager the manager to use for indirect reference
124        look-ups.
125        @param streamDict the stream dictionary map.
126        @return the filter list.
127        @throws PdfFormatException
128      */

129     protected static List getFilterList(PdfManager manager, Map streamDict) throws IOException, PdfFormatException {
130         
131         Object JavaDoc obj = streamDict.get(PDFNAME_FILTER);
132         if (PdfNull.isNull(obj)) {
133             return null;
134         }
135         if ( !(obj instanceof PdfObject) ) {
136             throw new PdfFormatException("Filter name is not a PDF object.");
137         }
138         obj = manager.getObjectIndirect((PdfObject)obj);
139         if (PdfNull.isNull(obj)) {
140             return null;
141         }
142         if ( ( !(obj instanceof PdfName) ) &&
143              ( !(obj instanceof PdfArray) ) ) {
144             throw new PdfFormatException("Filter name is not a name or array.");
145         }
146         List filters;
147         if (obj instanceof PdfArray) {
148             filters = new ArrayList( ((PdfArray)obj).getList() );
149         } else {
150             filters = new ArrayList();
151             filters.add( (PdfName)obj );
152         }
153         return filters;
154
155     }
156     
157 }
158
Popular Tags