PdfSmartCopy.java

1
/*
2
 * $Id: PdfSmartCopy.java 3991 2009-06-18 21:21:09Z psoares33 $
3
 *
4
 * Copyright 2007 Michael Neuweiler and Bruno Lowagie
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * (the "License"); you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9
 *
10
 * Software distributed under the License is distributed on an "AS IS" basis,
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
 * for the specific language governing rights and limitations under the License.
13
 *
14
 * The Original Code is 'iText, a free JAVA-PDF library'.
15
 *
16
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18
 * All Rights Reserved.
19
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21
 * 
22
 * This class was written by Michael Neuweiler based on hints given by Bruno Lowagie
23
 * 
24
 * Contributor(s): all the names of the contributors are added in the source code
25
 * where applicable.
26
 *
27
 * Alternatively, the contents of this file may be used under the terms of the
28
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
29
 * provisions of LGPL are applicable instead of those above.  If you wish to
30
 * allow use of your version of this file only under the terms of the LGPL
31
 * License and not to allow others to use your version of this file under
32
 * the MPL, indicate your decision by deleting the provisions above and
33
 * replace them with the notice and other provisions required by the LGPL.
34
 * If you do not delete the provisions above, a recipient may use your version
35
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36
 *
37
 * This library is free software; you can redistribute it and/or modify it
38
 * under the terms of the MPL as stated above or under the terms of the GNU
39
 * Library General Public License as published by the Free Software Foundation;
40
 * either version 2 of the License, or any later version.
41
 *
42
 * This library is distributed in the hope that it will be useful, but WITHOUT
43
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
44
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
45
 * details.
46
 *
47
 * If you didn't download this code from the following link, you should check if
48
 * you aren't using an obsolete version:
49
 * http://www.lowagie.com/iText/
50
 */
51
package com.lowagie.text.pdf;
52
53
import java.io.IOException;
54
import java.io.OutputStream;
55
import java.security.MessageDigest;
56
import java.util.Arrays;
57
import java.util.HashMap;
58
59
import com.lowagie.text.Document;
60
import com.lowagie.text.DocumentException;
61
import com.lowagie.text.ExceptionConverter;
62
63
64
/**
65
 * PdfSmartCopy has the same functionality as PdfCopy,
66
 * but when resources (such as fonts, images,...) are
67
 * encountered, a reference to these resources is saved
68
 * in a cache, so that they can be reused.
69
 * This requires more memory, but reduces the file size
70
 * of the resulting PDF document.
71
 */
72
73
public class PdfSmartCopy extends PdfCopy {
74
75
    /** the cache with the streams and references. */
76
    private HashMap streamMap = null;
77
78
    /** Creates a PdfSmartCopy instance. */
79
    public PdfSmartCopy(Document document, OutputStream os) throws DocumentException {
80
        super(document, os);
81
        this.streamMap = new HashMap();
82
    }
83
    /**
84
     * Translate a PRIndirectReference to a PdfIndirectReference
85
     * In addition, translates the object numbers, and copies the
86
     * referenced object to the output file if it wasn't available
87
     * in the cache yet. If it's in the cache, the reference to
88
     * the already used stream is returned.
89
     * 
90
     * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
91
     * file they came from, because each file has its own namespace. The translation
92
     * we do from their namespace to ours is *at best* heuristic, and guaranteed to
93
     * fail under some circumstances.
94
     */
95
    protected PdfIndirectReference copyIndirect(PRIndirectReference in) throws IOException, BadPdfFormatException {
96
        PdfObject srcObj = PdfReader.getPdfObjectRelease(in);
97
        ByteStore streamKey = null;
98
        boolean validStream = false;
99 1 1. copyIndirect : negated conditional → NO_COVERAGE
        if (srcObj.isStream()) {
100
            streamKey = new ByteStore((PRStream)srcObj);
101
            validStream = true;
102
            PdfIndirectReference streamRef = (PdfIndirectReference) streamMap.get(streamKey);
103 1 1. copyIndirect : negated conditional → NO_COVERAGE
            if (streamRef != null) {
104 1 1. copyIndirect : mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return streamRef;
105
            }
106
        }
107
108
        PdfIndirectReference theRef;
109
        RefKey key = new RefKey(in);
110
        IndirectReferences iRef = indirects.get(key);
111 1 1. copyIndirect : negated conditional → NO_COVERAGE
        if (iRef != null) {
112
            theRef = iRef.getRef();
113 1 1. copyIndirect : negated conditional → NO_COVERAGE
            if (iRef.getCopied()) {
114 1 1. copyIndirect : mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return theRef;
115
            }
116
        } else {
117
            theRef = body.getPdfIndirectReference();
118
            iRef = new IndirectReferences(theRef);
119
            indirects.put(key, iRef);
120
        }
121 1 1. copyIndirect : negated conditional → NO_COVERAGE
        if (srcObj.isDictionary()) {
122
            PdfObject type = PdfReader.getPdfObjectRelease(((PdfDictionary)srcObj).get(PdfName.TYPE));
123 1 1. copyIndirect : negated conditional → NO_COVERAGE
            if (PdfName.PAGE.equals(type)) {
124 1 1. copyIndirect : mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
                return theRef;
125
            }
126
        }
127 1 1. copyIndirect : removed call to com/lowagie/text/pdf/PdfCopy$IndirectReferences::setCopied → NO_COVERAGE
        iRef.setCopied();
128
129 1 1. copyIndirect : negated conditional → NO_COVERAGE
        if (validStream) {
130
            streamMap.put(streamKey, theRef);
131
        }
132
133
        PdfObject obj = copyObject(srcObj);
134
        addToBody(obj, theRef);
135 1 1. copyIndirect : mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return theRef;
136
    }
137
138
    static class ByteStore {
139
        private byte[] b;
140
        private int hash;
141
        private MessageDigest md5;
142
143
        private void serObject(PdfObject obj, int level, ByteBuffer bb) throws IOException {
144 2 1. serObject : changed conditional boundary → NO_COVERAGE
2. serObject : negated conditional → NO_COVERAGE
            if (level <= 0)
145
                return;
146 1 1. serObject : negated conditional → NO_COVERAGE
            if (obj == null) {
147
                bb.append("$Lnull");
148
                return;
149
            }
150
            obj = PdfReader.getPdfObject(obj);
151 1 1. serObject : negated conditional → NO_COVERAGE
            if (obj.isStream()) {
152
                bb.append("$B");
153 2 1. serObject : Replaced integer subtraction with addition → NO_COVERAGE
2. serObject : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serDic → NO_COVERAGE
                serDic((PdfDictionary)obj, level - 1, bb);
154 2 1. serObject : changed conditional boundary → NO_COVERAGE
2. serObject : negated conditional → NO_COVERAGE
                if (level > 0) {
155 1 1. serObject : removed call to java/security/MessageDigest::reset → NO_COVERAGE
                    md5.reset();
156
                    bb.append(md5.digest(PdfReader.getStreamBytesRaw((PRStream)obj)));
157
                }
158
            }
159 1 1. serObject : negated conditional → NO_COVERAGE
            else if (obj.isDictionary()) {
160 2 1. serObject : Replaced integer subtraction with addition → NO_COVERAGE
2. serObject : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serDic → NO_COVERAGE
                serDic((PdfDictionary)obj, level - 1, bb);
161
            }
162 1 1. serObject : negated conditional → NO_COVERAGE
            else if (obj.isArray()) {
163 2 1. serObject : Replaced integer subtraction with addition → NO_COVERAGE
2. serObject : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serArray → NO_COVERAGE
                serArray((PdfArray)obj, level - 1, bb);
164
            }
165 1 1. serObject : negated conditional → NO_COVERAGE
            else if (obj.isString()) {
166
                bb.append("$S").append(obj.toString());
167
            }
168 1 1. serObject : negated conditional → NO_COVERAGE
            else if (obj.isName()) {
169
                bb.append("$N").append(obj.toString());
170
            }
171
            else
172
                bb.append("$L").append(obj.toString());
173
        }
174
        
175
        private void serDic(PdfDictionary dic, int level, ByteBuffer bb) throws IOException {
176
            bb.append("$D");
177 2 1. serDic : changed conditional boundary → NO_COVERAGE
2. serDic : negated conditional → NO_COVERAGE
            if (level <= 0)
178
                return;
179
            Object[] keys = dic.getKeys().toArray();
180 1 1. serDic : removed call to java/util/Arrays::sort → NO_COVERAGE
            Arrays.sort(keys);
181
            for (Object key : keys) {
182 1 1. serDic : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE
                serObject((PdfObject) key, level, bb);
183 1 1. serDic : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE
                serObject(dic.get((PdfName) key), level, bb);
184
            }
185
        }
186
        
187
        private void serArray(PdfArray array, int level, ByteBuffer bb) throws IOException {
188
            bb.append("$A");
189 2 1. serArray : changed conditional boundary → NO_COVERAGE
2. serArray : negated conditional → NO_COVERAGE
            if (level <= 0)
190
                return;
191 2 1. serArray : changed conditional boundary → NO_COVERAGE
2. serArray : negated conditional → NO_COVERAGE
            for (int k = 0; k < array.size(); ++k) {
192 1 1. serArray : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE
                serObject(array.getPdfObject(k), level, bb);
193
            }
194
        }
195
        
196
        ByteStore(PRStream str) throws IOException {
197
            try {
198
                md5 = MessageDigest.getInstance("MD5");
199
            }
200
            catch (Exception e) {
201
                throw new ExceptionConverter(e);
202
            }
203
            ByteBuffer bb = new ByteBuffer();
204
            int level = 100;
205 1 1. : removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE
            serObject(str, level, bb);
206
            this.b = bb.toByteArray();
207
            md5 = null;
208
        }
209
210
        public boolean equals(Object obj) {
211 1 1. equals : negated conditional → NO_COVERAGE
            if (!(obj instanceof ByteStore))
212 1 1. equals : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
                return false;
213 1 1. equals : negated conditional → NO_COVERAGE
            if (hashCode() != obj.hashCode())
214 1 1. equals : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
                return false;
215 1 1. equals : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
            return Arrays.equals(b, ((ByteStore)obj).b);
216
        }
217
218
        public int hashCode() {
219
            if (hash == 0) {
220
                int len = b.length;
221
                for (byte b1 : b) {
222
                    hash = hash * 31 + (b1 & 0xff);
223
                }
224
            }
225
            return hash;
226
        }
227
    }
228
}

Mutations

99

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

103

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

104

1.1
Location : copyIndirect
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

111

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

113

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

114

1.1
Location : copyIndirect
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

121

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

123

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

124

1.1
Location : copyIndirect
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

127

1.1
Location : copyIndirect
Killed by : none
removed call to com/lowagie/text/pdf/PdfCopy$IndirectReferences::setCopied → NO_COVERAGE

129

1.1
Location : copyIndirect
Killed by : none
negated conditional → NO_COVERAGE

135

1.1
Location : copyIndirect
Killed by : none
mutated return of Object value for com/lowagie/text/pdf/PdfSmartCopy::copyIndirect to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

144

1.1
Location : serObject
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

146

1.1
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

151

1.1
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

153

1.1
Location : serObject
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : serObject
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serDic → NO_COVERAGE

154

1.1
Location : serObject
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

155

1.1
Location : serObject
Killed by : none
removed call to java/security/MessageDigest::reset → NO_COVERAGE

159

1.1
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

160

1.1
Location : serObject
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : serObject
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serDic → NO_COVERAGE

162

1.1
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

163

1.1
Location : serObject
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : serObject
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serArray → NO_COVERAGE

165

1.1
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

168

1.1
Location : serObject
Killed by : none
negated conditional → NO_COVERAGE

177

1.1
Location : serDic
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : serDic
Killed by : none
negated conditional → NO_COVERAGE

180

1.1
Location : serDic
Killed by : none
removed call to java/util/Arrays::sort → NO_COVERAGE

182

1.1
Location : serDic
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE

183

1.1
Location : serDic
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE

189

1.1
Location : serArray
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : serArray
Killed by : none
negated conditional → NO_COVERAGE

191

1.1
Location : serArray
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : serArray
Killed by : none
negated conditional → NO_COVERAGE

192

1.1
Location : serArray
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE

205

1.1
Location :
Killed by : none
removed call to com/lowagie/text/pdf/PdfSmartCopy$ByteStore::serObject → NO_COVERAGE

211

1.1
Location : equals
Killed by : none
negated conditional → NO_COVERAGE

212

1.1
Location : equals
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

213

1.1
Location : equals
Killed by : none
negated conditional → NO_COVERAGE

214

1.1
Location : equals
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

215

1.1
Location : equals
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2