KickJava   Java API By Example, From Geeks To Geeks.

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


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.nio.*;
9 import java.util.*;
10 import java.util.zip.*;
11 import com.etymon.pjx.*;
12
13 /**
14    Implements a stream filter for Flate compression (based on
15    zlib/deflate). This class is synchronized.
16    @author Nassib Nassar
17 */

18 public class PdfFlateFilter
19     implements PdfStreamFilter {
20
21         protected static final PdfName PDFNAME_FILTER = new PdfName("Filter");
22         protected static final PdfName PDFNAME_FLATEDECODE = new PdfName("FlateDecode");
23
24     /**
25        The manager associated with the PDF document.
26     */

27     protected PdfManager _m;
28
29     /**
30        A byte array used for buffering.
31     */

32     protected byte[] _ba = new byte[16384];
33
34     /**
35        Instance used for deflating.
36      */

37     Deflater _deflater = new Deflater(9);
38     
39     /**
40        Instance used for inflating.
41      */

42     Inflater _inflater = new Inflater();
43     
44     /**
45        Constructs an instance of this class with a specified
46        manager instance.
47        @param manager the manager instance.
48      */

49     public PdfFlateFilter(PdfManager manager) {
50         _m = manager;
51     }
52
53     public PdfName getName() {
54         return PDFNAME_FLATEDECODE;
55     }
56
57     public PdfStream encode(PdfStream stream) throws IOException, PdfFormatException {
58         synchronized (this) {
59             synchronized (_m) {
60         
61                 PdfManager m = _m;
62                 
63                 // add /FlateDecode to the pipeline
64
Map dict = new HashMap( stream.getDictionary().getMap() );
65                 List filters = PdfDecodeStream.getFilterList(m, dict);
66                 if (filters == null) {
67                     dict.put(PDFNAME_FILTER, PDFNAME_FLATEDECODE);
68                 } else {
69                     List newFilters = new ArrayList(filters.size() + 1);
70                     newFilters.add(PDFNAME_FLATEDECODE);
71                     newFilters.addAll(filters);
72                     dict.put(PDFNAME_FILTER, new PdfArray(newFilters));
73                 }
74                 
75                 // encode the stream
76
Deflater deflater = _deflater;
77                 ByteBuffer bb = stream.getBuffer();
78                 byte[] bba = new byte[bb.capacity()];
79                 bb.get(bba);
80                 deflater.setInput(bba);
81                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
82                 int len;
83                 byte[] ba = _ba;
84                 do {
85                     do {
86                         while ( (len = deflater.deflate(ba)) != 0) {
87                             baos.write(ba, 0, len);
88                         }
89                     } while (deflater.needsInput() == false);
90                     deflater.finish();
91                 } while (deflater.finished() == false);
92                 deflater.reset();
93                 baos.close();
94                 
95                 byte[] nbba = baos.toByteArray();
96                 ByteBuffer nbb = ByteBuffer.wrap(nbba);
97
98                 return new PdfStream( new PdfDictionary(dict),
99                               nbb );
100                 
101             }
102         }
103     }
104     
105     public PdfStream decode(PdfStream stream) throws IOException, PdfFormatException, PdfDecoderFormatException {
106         synchronized (this) {
107             synchronized (_m) {
108         
109                 PdfManager m = _m;
110                 
111                 // check that /FlateDecode is first in the pipeline
112
Map dict = new HashMap( stream.getDictionary().getMap() );
113                 List filters = PdfDecodeStream.getFilterList(m, dict);
114                 if ( (filters == null) ||
115                      ( ((PdfName)filters.get(0)).equals(PDFNAME_FLATEDECODE) == false) ) {
116                     throw new PdfDecoderFormatException("Incorrect decoding method for this stream.");
117                 }
118                 // now we have confirmed that /FlateDecode is first
119

120                 // remove /FlateDecode
121
PdfDecodeStream.modifyFilterList(filters, dict);
122                 
123                 // decode the stream
124
Inflater inflater = _inflater;
125                 ByteBuffer bb = stream.getBuffer();
126                 byte[] bba = new byte[bb.capacity()];
127                 bb.get(bba);
128                 inflater.setInput(bba);
129                 if (inflater.needsDictionary()) {
130                     throw new PdfDecoderFormatException("Flate compression needs dictionary.");
131                 }
132                 ByteArrayOutputStream baos = new ByteArrayOutputStream(bba.length);
133                 int len;
134                 byte[] ba = _ba;
135                 try {
136                     while ( (len = inflater.inflate(ba)) != 0) {
137                         baos.write(ba, 0, len);
138                     }
139                 } catch (DataFormatException e) {
140                     throw new PdfDecoderFormatException( e.getMessage() );
141                 }
142                 if (inflater.finished() == false) {
143                     throw new PdfDecoderFormatException("Flate decoder: unexpected end of stream.");
144                 }
145                 inflater.reset();
146                 baos.close();
147                 
148                 byte[] nbba = baos.toByteArray();
149                 ByteBuffer nbb = ByteBuffer.wrap(nbba);
150
151                 return new PdfStream( new PdfDictionary(dict),
152                               nbb );
153                 
154             }
155         }
156     }
157     
158 }
159
Popular Tags