KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfSmartCopy


1 /*
2  * $Id: PdfSmartCopy.java 2622 2007-02-23 21:14:35Z xlv $
3  * $Name$
4  *
5  * Copyright 2007 Michael Neuweiler and Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * This class was written by Michael Neuweiler based on hints given by Bruno Lowagie
24  *
25  * Contributor(s): all the names of the contributors are added in the source code
26  * where applicable.
27  *
28  * Alternatively, the contents of this file may be used under the terms of the
29  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
30  * provisions of LGPL are applicable instead of those above. If you wish to
31  * allow use of your version of this file only under the terms of the LGPL
32  * License and not to allow others to use your version of this file under
33  * the MPL, indicate your decision by deleting the provisions above and
34  * replace them with the notice and other provisions required by the LGPL.
35  * If you do not delete the provisions above, a recipient may use your version
36  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Library General Public License as published by the Free Software Foundation;
41  * either version 2 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
45  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46  * details.
47  *
48  * If you didn't download this code from the following link, you should check if
49  * you aren't using an obsolete version:
50  * http://www.lowagie.com/iText/
51  */

52 package com.lowagie.text.pdf;
53
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 import com.lowagie.text.Document;
59 import com.lowagie.text.DocumentException;
60 import java.util.Arrays JavaDoc;
61
62 /**
63  * PdfSmartCopy has the same functionality as PdfCopy,
64  * but when resources (such as fonts, images,...) are
65  * encountered, a reference to these resources is saved
66  * in a cache, so that they can be reused.
67  * This requires more memory, but reduces the file size
68  * of the resulting PDF document.
69  */

70
71 public class PdfSmartCopy extends PdfCopy {
72
73     /** the cache with the streams and references. */
74     private HashMap JavaDoc streamMap = null;
75
76     /** Creates a PdfSmartCopy instance. */
77     public PdfSmartCopy(Document document, OutputStream JavaDoc os) throws DocumentException {
78         super(document, os);
79         this.streamMap = new HashMap JavaDoc();
80     }
81     /**
82      * Translate a PRIndirectReference to a PdfIndirectReference
83      * In addition, translates the object numbers, and copies the
84      * referenced object to the output file if it wasn't available
85      * in the cache yet. If it's in the cache, the reference to
86      * the already used stream is returned.
87      *
88      * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
89      * file they came from, because each file has its own namespace. The translation
90      * we do from their namespace to ours is *at best* heuristic, and guaranteed to
91      * fail under some circumstances.
92      */

93     protected PdfIndirectReference copyIndirect(PRIndirectReference in) throws IOException JavaDoc, BadPdfFormatException {
94         PdfObject srcObj = PdfReader.getPdfObjectRelease(in);
95         ByteStore streamKey = null;
96         if (srcObj.type == PdfObject.STREAM) {
97             // Only the content and the key names is compared, probably the key values should also be compared
98
streamKey = new ByteStore((PRStream)srcObj);
99             PdfIndirectReference streamRef = (PdfIndirectReference) streamMap.get(streamKey);
100             if (streamRef != null) {
101                 return streamRef;
102             }
103         }
104
105         PdfIndirectReference theRef;
106         RefKey key = new RefKey(in);
107         IndirectReferences iRef = (IndirectReferences) indirects.get(key);
108         if (iRef != null) {
109             theRef = iRef.getRef();
110             if (iRef.getCopied()) {
111                 return theRef;
112             }
113         } else {
114             theRef = body.getPdfIndirectReference();
115             iRef = new IndirectReferences(theRef);
116             indirects.put(key, iRef);
117         }
118         iRef.setCopied();
119
120         if (srcObj.type == PdfObject.STREAM) {
121             streamMap.put(streamKey, theRef);
122         }
123
124         PdfObject obj = copyObject(srcObj);
125         addToBody(obj, theRef);
126         return theRef;
127     }
128
129     static class ByteStore {
130         private byte[] b;
131         private int hash;
132         
133         ByteStore(PRStream str) throws IOException JavaDoc {
134             byte[] streamContent = PdfReader.getStreamBytesRaw(str);
135             Object JavaDoc[] keys = str.getKeys().toArray();
136             Arrays.sort(keys);
137             ByteBuffer bb = new ByteBuffer();
138             for (int k = 0; k < keys.length; ++k) {
139                 bb.append(keys[k].toString());
140             }
141             bb.append(streamContent);
142             this.b = bb.toByteArray();
143         }
144
145         public boolean equals(Object JavaDoc obj) {
146             if (!(obj instanceof ByteStore))
147                 return false;
148             if (hashCode() != obj.hashCode())
149                 return false;
150             return Arrays.equals(b, ((ByteStore)obj).b);
151         }
152
153         public int hashCode() {
154             if (hash == 0) {
155                 int len = b.length;
156                 for (int k = 0; k < len; ++k) {
157                     hash = hash * 31 + (b[k] & 0xff);
158                 }
159             }
160             return hash;
161         }
162     }
163 }
Popular Tags