1 | /* | |
2 | * $Id: PdfEFStream.java 3735 2009-02-26 01:44:03Z xlv $ | |
3 | * | |
4 | * Copyright (c) 2008 by 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 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | ||
50 | package com.lowagie.text.pdf; | |
51 | ||
52 | import java.io.IOException; | |
53 | import java.io.InputStream; | |
54 | import java.io.OutputStream; | |
55 | import java.util.zip.Deflater; | |
56 | import java.util.zip.DeflaterOutputStream; | |
57 | ||
58 | /** | |
59 | * Extends PdfStream and should be used to create Streams for Embedded Files | |
60 | * (file attachments). | |
61 | * @since 2.1.3 | |
62 | */ | |
63 | ||
64 | public class PdfEFStream extends PdfStream { | |
65 | ||
66 | /** | |
67 | * Creates a Stream object using an InputStream and a PdfWriter object | |
68 | * @param in the InputStream that will be read to get the Stream object | |
69 | * @param writer the writer to which the stream will be added | |
70 | */ | |
71 | public PdfEFStream(InputStream in, PdfWriter writer) { | |
72 | super(in, writer); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Creates a Stream object using a byte array | |
77 | * @param fileStore the bytes for the stream | |
78 | */ | |
79 | public PdfEFStream(byte[] fileStore) { | |
80 | super(fileStore); | |
81 | } | |
82 | ||
83 | /** | |
84 | * @see com.lowagie.text.pdf.PdfDictionary#toPdf(com.lowagie.text.pdf.PdfWriter, java.io.OutputStream) | |
85 | */ | |
86 | public void toPdf(PdfWriter writer, OutputStream os) throws IOException { | |
87 |
2
1. toPdf : negated conditional → NO_COVERAGE 2. toPdf : negated conditional → NO_COVERAGE |
if (inputStream != null && compressed) |
88 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
put(PdfName.FILTER, PdfName.FLATEDECODE); |
89 | PdfEncryption crypto = null; | |
90 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (writer != null) |
91 | crypto = writer.getEncryption(); | |
92 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (crypto != null) { |
93 | PdfObject filter = get(PdfName.FILTER); | |
94 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (filter != null) { |
95 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (PdfName.CRYPT.equals(filter)) |
96 | crypto = null; | |
97 |
1
1. toPdf : negated conditional → NO_COVERAGE |
else if (filter.isArray()) { |
98 | PdfArray a = (PdfArray)filter; | |
99 |
2
1. toPdf : negated conditional → NO_COVERAGE 2. toPdf : negated conditional → NO_COVERAGE |
if (!a.isEmpty() && PdfName.CRYPT.equals(a.getPdfObject(0))) |
100 | crypto = null; | |
101 | } | |
102 | } | |
103 | } | |
104 |
2
1. toPdf : negated conditional → NO_COVERAGE 2. toPdf : negated conditional → NO_COVERAGE |
if (crypto != null && crypto.isEmbeddedFilesOnly()) { |
105 | PdfArray filter = new PdfArray(); | |
106 | PdfArray decodeparms = new PdfArray(); | |
107 | PdfDictionary crypt = new PdfDictionary(); | |
108 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE |
crypt.put(PdfName.NAME, PdfName.STDCF); |
109 | filter.add(PdfName.CRYPT); | |
110 | decodeparms.add(crypt); | |
111 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (compressed) { |
112 | filter.add(PdfName.FLATEDECODE); | |
113 | decodeparms.add(new PdfNull()); | |
114 | } | |
115 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
put(PdfName.FILTER, filter); |
116 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
put(PdfName.DECODEPARMS, decodeparms); |
117 | } | |
118 | PdfObject nn = get(PdfName.LENGTH); | |
119 |
3
1. toPdf : negated conditional → NO_COVERAGE 2. toPdf : negated conditional → NO_COVERAGE 3. toPdf : negated conditional → NO_COVERAGE |
if (crypto != null && nn != null && nn.isNumber()) { |
120 | int sz = ((PdfNumber)nn).intValue(); | |
121 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
put(PdfName.LENGTH, new PdfNumber(crypto.calculateStreamSize(sz))); |
122 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::superToPdf → NO_COVERAGE |
superToPdf(writer, os); |
123 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
put(PdfName.LENGTH, nn); |
124 | } | |
125 | else | |
126 |
1
1. toPdf : removed call to com/lowagie/text/pdf/PdfEFStream::superToPdf → NO_COVERAGE |
superToPdf(writer, os); |
127 | ||
128 |
1
1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE |
os.write(STARTSTREAM); |
129 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (inputStream != null) { |
130 | rawLength = 0; | |
131 | DeflaterOutputStream def = null; | |
132 | OutputStreamCounter osc = new OutputStreamCounter(os); | |
133 | OutputStreamEncryption ose = null; | |
134 | OutputStream fout = osc; | |
135 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (crypto != null) |
136 | fout = ose = crypto.getEncryptionStream(fout); | |
137 | Deflater deflater = null; | |
138 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (compressed) { |
139 | deflater = new Deflater(compressionLevel); | |
140 | fout = def = new DeflaterOutputStream(fout, deflater, 0x8000); | |
141 | } | |
142 | ||
143 | byte[] buf = new byte[4192]; | |
144 | while (true) { | |
145 | int n = inputStream.read(buf); | |
146 |
2
1. toPdf : changed conditional boundary → NO_COVERAGE 2. toPdf : negated conditional → NO_COVERAGE |
if (n <= 0) |
147 | break; | |
148 |
1
1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE |
fout.write(buf, 0, n); |
149 |
1
1. toPdf : Replaced integer addition with subtraction → NO_COVERAGE |
rawLength += n; |
150 | } | |
151 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (def != null) { |
152 |
1
1. toPdf : removed call to java/util/zip/DeflaterOutputStream::finish → NO_COVERAGE |
def.finish(); |
153 |
1
1. toPdf : removed call to java/util/zip/Deflater::end → NO_COVERAGE |
deflater.end(); |
154 | } | |
155 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (ose != null) |
156 |
1
1. toPdf : removed call to com/lowagie/text/pdf/OutputStreamEncryption::finish → NO_COVERAGE |
ose.finish(); |
157 | inputStreamLength = osc.getCounter(); | |
158 | } | |
159 | else { | |
160 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (crypto == null) { |
161 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (streamBytes != null) |
162 |
1
1. toPdf : removed call to java/io/ByteArrayOutputStream::writeTo → NO_COVERAGE |
streamBytes.writeTo(os); |
163 | else | |
164 |
1
1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE |
os.write(bytes); |
165 | } | |
166 | else { | |
167 | byte[] b; | |
168 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (streamBytes != null) { |
169 | b = crypto.encryptByteArray(streamBytes.toByteArray()); | |
170 | } | |
171 | else { | |
172 | b = crypto.encryptByteArray(bytes); | |
173 | } | |
174 |
1
1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE |
os.write(b); |
175 | } | |
176 | } | |
177 |
1
1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE |
os.write(ENDSTREAM); |
178 | } | |
179 | } | |
Mutations | ||
87 |
1.1 2.2 |
|
88 |
1.1 |
|
90 |
1.1 |
|
92 |
1.1 |
|
94 |
1.1 |
|
95 |
1.1 |
|
97 |
1.1 |
|
99 |
1.1 2.2 |
|
104 |
1.1 2.2 |
|
108 |
1.1 |
|
111 |
1.1 |
|
115 |
1.1 |
|
116 |
1.1 |
|
119 |
1.1 2.2 3.3 |
|
121 |
1.1 |
|
122 |
1.1 |
|
123 |
1.1 |
|
126 |
1.1 |
|
128 |
1.1 |
|
129 |
1.1 |
|
135 |
1.1 |
|
138 |
1.1 |
|
146 |
1.1 2.2 |
|
148 |
1.1 |
|
149 |
1.1 |
|
151 |
1.1 |
|
152 |
1.1 |
|
153 |
1.1 |
|
155 |
1.1 |
|
156 |
1.1 |
|
160 |
1.1 |
|
161 |
1.1 |
|
162 |
1.1 |
|
164 |
1.1 |
|
168 |
1.1 |
|
174 |
1.1 |
|
177 |
1.1 |