1 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 18 public final class PdfWriter { 19 20 23 protected BufferedOutputStream _bos; 24 25 28 protected DataOutputStream _dos; 29 30 33 protected FileChannel _fc; 34 35 38 protected FileOutputStream _fos; 39 40 43 protected static final String PDF_HEADER = "%PDF-" + PdfWriter.PDF_VERSION + "\n%\323\343\317\342\n"; 44 45 48 protected static final String PDF_VERSION = "1.4"; 49 50 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 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 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 113 protected DataOutputStream getDataOutputStream() { 114 synchronized (this) { 115 return _dos; 116 } 117 } 118 119 123 protected FileChannel getFileChannel() { 124 synchronized (this) { 125 return _fc; 126 } 127 } 128 129 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 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 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 202 public int writeHeader() throws IOException { 203 synchronized (this) { 204 String header = PdfWriter.PDF_HEADER; 205 _dos.writeBytes(header); 206 return header.length(); 207 } 208 } 209 210 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 234 public int writeObjectIndirect(PdfObject obj, int number, int generation) throws IOException { 235 synchronized (this) { 236 237 String 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 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 |