KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > FlateFilter


1 /*
2  * $Id: FlateFilter.java,v 1.2.2.1 2003/02/25 14:29:37 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.pdf;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.util.zip.DeflaterOutputStream JavaDoc;
56
57 /**
58  * A filter to deflate a stream. Note that the attributes for
59  * prediction, colors, bitsPerComponent, and columns are not supported
60  * when this filter is used to handle the data compression. They are
61  * only valid for externally encoded data such as that from a graphics
62  * file.
63  */

64 public class FlateFilter extends PDFFilter {
65
66     public static final int PREDICTION_NONE = 1;
67     public static final int PREDICTION_TIFF2 = 2;
68     public static final int PREDICTION_PNG_NONE = 10;
69     public static final int PREDICTION_PNG_SUB = 11;
70     public static final int PREDICTION_PNG_UP = 12;
71     public static final int PREDICTION_PNG_AVG = 13;
72     public static final int PREDICTION_PNG_PAETH = 14;
73     public static final int PREDICTION_PNG_OPT = 15;
74
75
76     private int _predictor = PREDICTION_NONE;
77     private int _colors;
78     private int _bitsPerComponent;
79     private int _columns;
80
81     public String JavaDoc getName() {
82         return "/FlateDecode";
83     }
84
85     public String JavaDoc getDecodeParms() {
86         if (_predictor > PREDICTION_NONE) {
87             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
88             sb.append("<< /Predictor ");
89             sb.append(_predictor);
90             if (_colors > 0) {
91                 sb.append(" /Colors " + _colors);
92             }
93             if (_bitsPerComponent > 0) {
94                 sb.append(" /BitsPerComponent " + _bitsPerComponent);
95             }
96             if (_columns > 0) {
97                 sb.append(" /Columns " + _columns);
98             }
99             sb.append(" >> ");
100             return sb.toString();
101         }
102         return null;
103     }
104
105
106     /**
107      * Encode the given data and return it. Note: a side effect of
108      * this method is that it resets the prediction to the default
109      * because these attributes are not supported. So the DecodeParms
110      * should be retrieved after calling this method.
111      */

112     public byte[] encode(byte[] data) {
113         ByteArrayOutputStream JavaDoc outArrayStream = new ByteArrayOutputStream JavaDoc();
114         _predictor = PREDICTION_NONE;
115         try {
116             DeflaterOutputStream JavaDoc compressedStream =
117                 new DeflaterOutputStream JavaDoc(outArrayStream);
118             compressedStream.write(data, 0, data.length);
119             compressedStream.flush();
120             compressedStream.close();
121         } catch (IOException JavaDoc e) {
122             org.apache.fop.messaging.MessageHandler.error("Fatal error: "
123                     + e.getMessage());
124             e.printStackTrace();
125         }
126
127         return outArrayStream.toByteArray();
128     }
129
130     public void setPredictor(int predictor) throws PDFFilterException {
131         _predictor = predictor;
132
133     }
134
135     public int getPredictor() {
136         return _predictor;
137     }
138
139
140     public void setColors(int colors) throws PDFFilterException {
141         if (_predictor != PREDICTION_NONE) {
142             _colors = colors;
143         } else {
144             throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set Colors");
145         }
146     }
147
148     public int getColors() {
149         return _colors;
150     }
151
152
153     public void setBitsPerComponent(int bits) throws PDFFilterException {
154         if (_predictor != PREDICTION_NONE) {
155             _bitsPerComponent = bits;
156         } else {
157             throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set bitsPerComponent");
158         }
159     }
160
161     public int getBitsPerComponent() {
162         return _bitsPerComponent;
163     }
164
165
166     public void setColumns(int columns) throws PDFFilterException {
167         if (_predictor != PREDICTION_NONE) {
168             _columns = columns;
169         } else {
170             throw new PDFFilterException("Prediction must not be PREDICTION_NONE in order to set Columns");
171         }
172     }
173
174     public int getColumns() {
175         return _columns;
176     }
177
178
179 }
180
Popular Tags