KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfWriter


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

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8 import java.nio.*;
9 import java.nio.channels.*;
10 import java.util.*;
11
12 /**
13    Writes a PDF document. Most applications do not need to access
14    methods in this class but should instead go through {@link
15    PdfManager PdfManager}. This class is synchronized.
16    @author Nassib Nassar
17 */

18 public final class PdfWriter {
19
20     /**
21        The buffered output stream associated with the PDF document.
22     */

23     protected BufferedOutputStream _bos;
24
25     /**
26        The data output stream associated with the PDF document.
27     */

28     protected DataOutputStream _dos;
29
30     /**
31        The file channel associated with the PDF document.
32     */

33     protected FileChannel _fc;
34
35     /**
36        The file output stream associated with the PDF document.
37     */

38     protected FileOutputStream _fos;
39
40     /**
41        The header to write at the beginning of PDF files.
42     */

43     protected static final String JavaDoc PDF_HEADER = "%PDF-" + PdfWriter.PDF_VERSION + "\n%\323\343\317\342\n";
44     
45     /**
46        The PDF version output by this package.
47     */

48     protected static final String JavaDoc PDF_VERSION = "1.4";
49
50         /**
51            Creates a writer for a PDF document to be written to a
52            file. If the file exists, this constructor overwrites it.
53            @param pdfFile the file to write the PDF document to.
54            @throws IOException
55         */

56     public PdfWriter(File pdfFile) throws IOException {
57
58         _fos = new FileOutputStream(pdfFile);
59         _bos = new BufferedOutputStream(_fos);
60         _dos = new DataOutputStream(_bos);
61         _fc = _fos.getChannel();
62     }
63     
64         /**
65            Creates a writer for a PDF document to be written to an
66            <code>OutputStream</code>. The calling method should
67            ensure that the specified stream is a buffered stream, if
68            applicable.
69            @param outputStream the stream to write the PDF document to.
70            @throws IOException
71         */

72     public PdfWriter(OutputStream outputStream) throws IOException {
73
74         _fos = null;
75         _bos = null;
76         _dos = new DataOutputStream(outputStream);
77         _fc = null;
78     }
79
80     /**
81            Closes the PDF document and releases any system resources
82            associated with it.
83            @throws IOException
84         */

85         public void close() throws IOException {
86         synchronized (this) {
87             if (_dos != null) {
88                 _dos.flush();
89                 if (_fc != null) {
90                     _fc.close();
91                     _fc = null;
92                 }
93                 if (_dos != null) {
94                     _dos.close();
95                     _dos = null;
96                 }
97                 if (_bos != null) {
98                     _bos.close();
99                     _bos = null;
100                 }
101                 if (_fos != null) {
102                     _fos.close();
103                     _fos = null;
104                 }
105             }
106         }
107         }
108
109     /**
110        Returns the data output stream associated with this writer.
111        @return the data output stream.
112      */

113     protected DataOutputStream getDataOutputStream() {
114         synchronized (this) {
115             return _dos;
116         }
117     }
118     
119     /**
120        Returns the file channel associated with this writer.
121        @return the file channel.
122      */

123     protected FileChannel getFileChannel() {
124         synchronized (this) {
125             return _fc;
126         }
127     }
128
129     /**
130        Writes the contents of a <code>ByteBuffer</code> to the PDF
131        document. The entire capacity of the buffer is written,
132        disregarding its position and limit.
133        @return the number of bytes written.
134        @deprecated It needs to be considered whether this method
135        should write the entire capacity of the buffer, if it is to
136        be made a public method.
137      */

138     protected int writeByteBuffer(ByteBuffer byteBuffer) throws IOException {
139         byteBuffer.position(0);
140         byteBuffer.limit(byteBuffer.capacity());
141         if (_fc != null) {
142             _dos.flush();
143             _fc.write(byteBuffer);
144         } else {
145             byte[] b = new byte[byteBuffer.capacity()];
146             byteBuffer.get(b);
147             _dos.write(b);
148         }
149         return byteBuffer.capacity();
150     }
151
152     /**
153        Writes an entire PDF document stream to the output PDF
154        document. This is used for incremental update.
155        @param reader the source of the PDF document stream.
156        @return the number of bytes written.
157        @throws IOException
158      */

159     public long writeCopy(PdfReader reader) throws IOException {
160         synchronized (this) {
161             synchronized (reader) {
162
163                 PdfInput pdfInput = reader.getPdfInput();
164                 long inputLen = pdfInput.getLength();
165                 
166                 if (inputLen <= Integer.MAX_VALUE) {
167
168                     return writeByteBuffer( pdfInput.readBytes(0, inputLen) );
169
170                 } else {
171
172                     // huge files have to be
173
// written in blocks, since
174
// Java buffers use int
175
// instead of long; we should
176
// probably do something like
177
// this for normal files as
178
// well, to use less memory
179
int x = 0;
180                     long count = 0;
181                     while (x < inputLen) {
182
183                         count += writeByteBuffer(
184                             pdfInput.readBytes(x, Math.min(x + 65536,
185                                                inputLen) ) );
186                         x += 65536;
187
188                     }
189                     return count;
190                     
191                 }
192                 
193             }
194         }
195     }
196
197     /**
198        Writes a PDF header to the document.
199        @return the number of bytes written.
200        @throws IOException
201      */

202     public int writeHeader() throws IOException {
203         synchronized (this) {
204             String JavaDoc header = PdfWriter.PDF_HEADER;
205             _dos.writeBytes(header);
206             return header.length();
207         }
208     }
209     
210     /**
211        Writes a PDF object to the document as an indirect object.
212        @param obj the object to write.
213        @param number the object number.
214        @param generation the generation number.
215        @return the number of bytes written.
216        @throws IOException
217      */

218     public int writeObject(PdfObject obj, int number, int generation) throws IOException {
219         synchronized (this) {
220
221             return obj.writePdf(this, false);
222
223         }
224     }
225
226     /**
227        Writes a PDF object to the document as an indirect object.
228        @param obj the object to write.
229        @param number the object number.
230        @param generation the generation number.
231        @return the number of bytes written.
232        @throws IOException
233      */

234     public int writeObjectIndirect(PdfObject obj, int number, int generation) throws IOException {
235         synchronized (this) {
236
237             String JavaDoc s, t;
238             int count = 0;
239             
240             if (number != -1) {
241                 s = Integer.toString(number) + " " +
242                     Integer.toString(generation) + " obj\n";
243                 _dos.writeBytes(s);
244                 count += s.length();
245             }
246
247             count += writeObject(obj, number, generation);
248
249             if (number != -1) {
250                 _dos.writeBytes("\nendobj\n");
251                 count += 8;
252             }
253
254             return count;
255
256         }
257     }
258
259     /**
260        Writes a cross-reference table (and associated trailer) to
261        the document.
262        @param xt the cross-reference table.
263        @param startxref the file offset of the beginning of the
264        cross-reference table.
265        @return the number of bytes written.
266        @throws IOException
267      */

268     public int writeXrefTable(XrefTable xt, long startxref) throws IOException {
269         synchronized (this) {
270             return xt.writePdf(this, startxref);
271         }
272     }
273     
274 }
275
Popular Tags