KickJava   Java API By Example, From Geeks To Geeks.

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


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    Provides commonly used stream encoding functions. This class is
13    synchronized.
14    @author Nassib Nassar
15 */

16 public class PdfEncodeStream {
17
18         protected static final PdfName PDFNAME_FILTER = new PdfName("Filter");
19     protected static final PdfName PDFNAME_FLATEDECODE = new PdfName("FlateDecode");
20     protected static final PdfName PDFNAME_LZWDECODE = new PdfName("LZWDecode");
21     protected static final PdfName PDFNAME_RUNLENGTHDECODE = new PdfName("RunLengthDecode");
22     protected static final PdfName PDFNAME_CCITTFAXDECODE = new PdfName("CCITTFaxDecode");
23     protected static final PdfName PDFNAME_JBIG2DECODE = new PdfName("JBIG2Decode");
24     protected static final PdfName PDFNAME_DCTDECODE = new PdfName("DCTDecode");
25
26     /**
27        The set of stream filters used for compression.
28      */

29     protected Set _compressionFilters;
30
31     /**
32      */

33     protected PdfFlateFilter _flateFilter;
34     
35     /**
36        The manager associated with the PDF document.
37     */

38     protected PdfManager _m;
39
40     /**
41        Constructs an instance of this class with a specified
42        manager.
43        @param manager the manager instance.
44      */

45     public PdfEncodeStream(PdfManager manager) {
46         _m = manager;
47         _flateFilter = new PdfFlateFilter(manager);
48
49         _compressionFilters = new HashSet(6);
50         _compressionFilters.add(PDFNAME_FLATEDECODE);
51         _compressionFilters.add(PDFNAME_LZWDECODE);
52         _compressionFilters.add(PDFNAME_RUNLENGTHDECODE);
53         _compressionFilters.add(PDFNAME_CCITTFAXDECODE);
54         _compressionFilters.add(PDFNAME_JBIG2DECODE);
55         _compressionFilters.add(PDFNAME_DCTDECODE);
56     }
57     
58     /**
59        Encodes a stream using the Flate compression method (based
60        on zlib/deflate). If the stream is already compressed with
61        the Flate, LZW, RunLength, CCITTFax, JBIG2, or DCT method,
62        then this method returns the original stream unmodified.
63        This method uses {@link PdfFlateFilter#encode(PdfStream)
64        PdfFlateFilter.encode(PdfStream)} to perform the encoding.
65        @param stream the stream to encode.
66        @return the encoded stream, or the original stream if it is
67        already compressed.
68        @throws IOException
69        @throws PdfFormatException
70      */

71     public PdfStream compressFlate(PdfStream stream) throws IOException, PdfFormatException {
72         synchronized (this) {
73             synchronized (_m) {
74                 
75                 PdfManager m = _m;
76
77                 // get the set of filters already used
78
// on this stream, and if any of them
79
// are compression filters, simply
80
// return the original stream
81
Object JavaDoc obj = stream.getDictionary().getMap().get(PDFNAME_FILTER);
82                 if (PdfNull.isNull(obj) == false) {
83                     if ( !(obj instanceof PdfObject) ) {
84                         throw new PdfFormatException("Filter name is not a PDF object.");
85                     }
86                     obj = m.getObjectIndirect((PdfObject)obj);
87                     if (PdfNull.isNull(obj) == false) {
88                         Set filters;
89                         if (obj instanceof PdfArray) {
90                             filters = new HashSet( ((PdfArray)obj).getList() );
91                         } else {
92                             filters = new HashSet();
93                             filters.add( obj );
94                         }
95                         filters.retainAll(_compressionFilters);
96                         if (filters.isEmpty() == false) {
97                             return stream;
98                         }
99                     }
100                 }
101
102                 // now compress the stream with Flate
103
return _flateFilter.encode(stream);
104                 
105             }
106         }
107     }
108     
109 }
110
Popular Tags