KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFStream


1 /*
2  * $Id: PDFStream.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
3  *
4  * $Date: 2001/10/29 19:51:08 $
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package gnu.jpdf;
21
22 import java.io.*;
23 import java.util.*;
24 import java.util.zip.*;
25
26 /**
27  * This class implements a PDF stream object. In PDF, streams contain data
28  * like the graphic operators that render a page, or the pixels of an image.
29  *
30  * <p>In PDF, a stream can be compressed using several different methods, or
31  * left uncompressed. Here we support both uncompressed, and FlateDecode as
32  * it's supported by the java core.
33  *
34  * @author Peter T Mount http://www.retep.org.uk/pdf/
35  * @author Eric Z. Beard, ericzbeard@hotmail.com
36  * @author $Author: ezb $
37  * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
38  *
39  */

40 public class PDFStream extends PDFObject implements Serializable
41 {
42
43
44   /*
45    * NOTE: The original class is the work of Peter T. Mount, who released it
46    * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
47    * follows:
48    * The package name was changed to gnu.jpdf.
49    * The formatting was changed a little bit.
50    * It is still licensed under the LGPL.
51    */

52
53     /**
54      * This holds the stream's content.
55      */

56     transient ByteArrayOutputStream buf;
57     
58     /**
59      * True if we will compress the stream in the pdf file
60      */

61     boolean deflate;
62     
63     /**
64      * Constructs a plain stream.
65      * <p>By default, the stream will be compressed.
66      */

67     public PDFStream() {
68         this(null);
69     }
70     
71     /**
72      * Constructs a stream. The supplied type is stored in the stream's header
73      * and is used by other objects that extend the PDFStream class (like
74      * PDFImage).
75      * <p>By default, the stream will be compressed.
76      * @param type type for the stream
77      * @see PDFImage
78      */

79     public PDFStream(String JavaDoc type) {
80         super(type);
81         buf = new ByteArrayOutputStream();
82         
83         // default deflate mode
84
deflate = false;
85     }
86     
87     /**
88      * @param mode true will FlatDecode the stream
89      */

90     public void setDeflate(boolean mode) {
91         deflate = mode;
92     }
93     
94     /**
95      * Returs true if the stream will be compressed.
96      * @return true if compression is enabled
97      */

98     public boolean getDeflate() {
99         return deflate;
100     }
101     
102     /**
103      * Returns the OutputStream that will append to this stream.
104      * @return The stream for this object
105      */

106     public OutputStream getOutputStream() {
107         return (OutputStream)buf;
108     }
109     
110     /**
111      * Creates a PrintWriter that will append to this stream.
112      * @return a PrintWriter to write to the stream
113      */

114     public PrintWriter getWriter() {
115         return new PrintWriter(buf,true);
116     }
117     
118     /**
119      * This is for extenders, and provides access to the stream.
120      * @return ByteArrayOutputStream containing the contents.
121      */

122     public ByteArrayOutputStream getStream() {
123         return buf;
124     }
125     
126     /**
127      * @param os OutputStream to send the object to
128      * @exception IOException on error
129      */

130     public void write(OutputStream os) throws IOException {
131         writeStart(os);
132         writeStream(os);
133         // Unlike most PDF objects, we dont call writeEnd(os) because we
134
// contain a stream
135
}
136     
137     /**
138      * This inserts the Streams length, then the actual stream, finally
139      * the end of stream/object markers.
140      *
141      * <p>This is intended for anyone extending PDFStream, as objects
142      * containing streams do no use writeEnd(), and they must be able
143      * to write the actual stream.
144      *
145      * @param os OutputStream to send the object to
146      * @exception IOException on error
147      */

148     public void writeStream(OutputStream os) throws IOException {
149         if(deflate) {
150             ByteArrayOutputStream b = new ByteArrayOutputStream();
151             DeflaterOutputStream dos = new DeflaterOutputStream(b);
152             //,new Deflater(Deflater.BEST_COMPRESSION,true));
153
buf.writeTo(dos);
154             dos.finish();
155             dos.close();
156             
157             // FlatDecode is compatible with the java.util.zip.Deflater class
158
os.write("/Filter /FlateDecode\n".getBytes());
159             os.write("/Length ".getBytes());
160             os.write(Integer.toString(b.size()+1).getBytes());
161             os.write("\n>>\nstream\n".getBytes());
162             b.writeTo(os);
163             os.write("\n".getBytes());
164         } else {
165             // This is a non-deflated stream
166
os.write("/Length ".getBytes());
167             os.write(Integer.toString(buf.size()).getBytes());
168             os.write("\n>>\nstream\n".getBytes());
169             buf.writeTo(os);
170         }
171         
172         os.write("endstream\nendobj\n".getBytes());
173         
174         // Unlike most PDF objects, we dont call writeEnd(os) because we
175
// contain a stream
176
}
177
178
179
180   // Why is this here? Did it have a specific purpose?
181

182     
183     /**
184      * This implements our own special Serialization for this object.
185      *
186      * <p>Here we write the length of the stream's contents, then a byte
187      * array of the contents. We have to do this, as ByteArrayOutputStream
188      * is not serializable (hence the transient tag).
189      *
190      */

191     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException
192     {
193         out.writeInt(buf.size());
194         out.write(buf.toByteArray());
195     }
196     
197     /**
198      * This implements our own special Serialization for this object
199      *
200      * <p>Here we read the length of the stream's contents, then a byte
201      * array of the contents. Then we recreate a new ByteArrayOutputStream.
202      * We have to do this, as ByteArrayOutputStream is not serializable
203      * (hence the transient tag).
204      *
205      */

206     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException
207     {
208         int l = in.readInt();
209         byte b[] = new byte[l];
210         in.read(b,0,l);
211         buf=new ByteArrayOutputStream(l);
212         buf.write(b);
213     }
214     
215 }
216
Popular Tags