PRStream.java

1
/*
2
 * $Id: PRStream.java 3633 2008-12-23 18:42:06Z xlv $
3
 *
4
 * Copyright 2001, 2002 by Paulo Soares.
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.ByteArrayOutputStream;
53
import java.io.IOException;
54
import java.io.OutputStream;
55
import java.util.zip.Deflater;
56
import java.util.zip.DeflaterOutputStream;
57
58
import com.lowagie.text.Document;
59
import com.lowagie.text.ExceptionConverter;
60
61
62
public class PRStream extends PdfStream {
63
    
64
    protected PdfReader reader;
65
    protected int offset;
66
    protected int length;
67
    
68
    //added by ujihara for decryption
69
    protected int objNum = 0;
70
    protected int objGen = 0;
71
    
72
    public PRStream(PRStream stream, PdfDictionary newDic) {
73
        reader = stream.reader;
74
        offset = stream.offset;
75
        length = stream.length;
76
        compressed = stream.compressed;
77
        compressionLevel = stream.compressionLevel;
78
        streamBytes = stream.streamBytes;
79
        bytes = stream.bytes;
80
        objNum = stream.objNum;
81
        objGen = stream.objGen;
82 1 1. : negated conditional → NO_COVERAGE
        if (newDic != null)
83 1 1. : removed call to com/lowagie/text/pdf/PRStream::putAll → NO_COVERAGE
            putAll(newDic);
84
        else
85 1 1. : removed call to java/util/Map::putAll → NO_COVERAGE
            hashMap.putAll(stream.hashMap);
86
    }
87
88
    public PRStream(PRStream stream, PdfDictionary newDic, PdfReader reader) {
89
        this(stream, newDic);
90
        this.reader = reader;
91
    }
92
93
    public PRStream(PdfReader reader, int offset) {
94
        this.reader = reader;
95
        this.offset = offset;
96
    }
97
98
    public PRStream(PdfReader reader, byte[] conts) {
99
        this(reader, conts, DEFAULT_COMPRESSION);
100
    }
101
102
    /**
103
     * Creates a new PDF stream object that will replace a stream
104
     * in a existing PDF file.
105
     * @param    reader    the reader that holds the existing PDF
106
     * @param    conts    the new content
107
     * @param    compressionLevel    the compression level for the content
108
     * @since    2.1.3 (replacing the existing constructor without param compressionLevel)
109
     */
110
    public PRStream(PdfReader reader, byte[] conts, int compressionLevel) {
111
        this.reader = reader;
112
        this.offset = -1;
113 1 1. : negated conditional → NO_COVERAGE
        if (Document.compress) {
114
            try {
115
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
116
                Deflater deflater = new Deflater(compressionLevel);
117
                DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
118 1 1. : removed call to java/util/zip/DeflaterOutputStream::write → NO_COVERAGE
                zip.write(conts);
119 1 1. : removed call to java/util/zip/DeflaterOutputStream::close → NO_COVERAGE
                zip.close();
120 1 1. : removed call to java/util/zip/Deflater::end → NO_COVERAGE
                deflater.end();
121
                bytes = stream.toByteArray();
122
            }
123
            catch (IOException ioe) {
124
                throw new ExceptionConverter(ioe);
125
            }
126 1 1. : removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE
            put(PdfName.FILTER, PdfName.FLATEDECODE);
127
        }
128
        else
129
            bytes = conts;
130 1 1. : removed call to com/lowagie/text/pdf/PRStream::setLength → NO_COVERAGE
        setLength(bytes.length);
131
    }
132
    
133
    /**
134
     * Sets the data associated with the stream, either compressed or
135
     * uncompressed. Note that the data will never be compressed if
136
     * Document.compress is set to false.
137
     * 
138
     * @param data raw data, decrypted and uncompressed.
139
     * @param compress true if you want the stream to be compressed.
140
     * @since    iText 2.1.1
141
     */
142
    public void setData(byte[] data, boolean compress) {
143 1 1. setData : removed call to com/lowagie/text/pdf/PRStream::setData → NO_COVERAGE
        setData(data, compress, DEFAULT_COMPRESSION);
144
    }
145
    
146
    /**
147
     * Sets the data associated with the stream, either compressed or
148
     * uncompressed. Note that the data will never be compressed if
149
     * Document.compress is set to false.
150
     * 
151
     * @param data raw data, decrypted and uncompressed.
152
     * @param compress true if you want the stream to be compressed.
153
     * @param compressionLevel    a value between -1 and 9 (ignored if compress == false)
154
     * @since    iText 2.1.3
155
     */
156
    public void setData(byte[] data, boolean compress, int compressionLevel) {
157 1 1. setData : removed call to com/lowagie/text/pdf/PRStream::remove → NO_COVERAGE
        remove(PdfName.FILTER);
158
        this.offset = -1;
159 2 1. setData : negated conditional → NO_COVERAGE
2. setData : negated conditional → NO_COVERAGE
        if (Document.compress && compress) {
160
            try {
161
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
162
                Deflater deflater = new Deflater(compressionLevel);
163
                DeflaterOutputStream zip = new DeflaterOutputStream(stream, deflater);
164 1 1. setData : removed call to java/util/zip/DeflaterOutputStream::write → NO_COVERAGE
                zip.write(data);
165 1 1. setData : removed call to java/util/zip/DeflaterOutputStream::close → NO_COVERAGE
                zip.close();
166 1 1. setData : removed call to java/util/zip/Deflater::end → NO_COVERAGE
                deflater.end();
167
                bytes = stream.toByteArray();
168
                this.compressionLevel = compressionLevel;
169
            }
170
            catch (IOException ioe) {
171
                throw new ExceptionConverter(ioe);
172
            }
173 1 1. setData : removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE
            put(PdfName.FILTER, PdfName.FLATEDECODE);
174
        }
175
        else
176
            bytes = data;
177 1 1. setData : removed call to com/lowagie/text/pdf/PRStream::setLength → NO_COVERAGE
        setLength(bytes.length);
178
    }
179
    
180
    /**Sets the data associated with the stream
181
     * @param data raw data, decrypted and uncompressed.
182
     */
183
    public void setData(byte[] data) {
184 1 1. setData : removed call to com/lowagie/text/pdf/PRStream::setData → NO_COVERAGE
        setData(data, true);
185
    }
186
187
    public void setLength(int length) {
188
        this.length = length;
189 1 1. setLength : removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE
        put(PdfName.LENGTH, new PdfNumber(length));
190
    }
191
    
192
    public int getOffset() {
193
        return offset;
194
    }
195
    
196
    public int getLength() {
197
        return length;
198
    }
199
    
200
    public PdfReader getReader() {
201
        return reader;
202
    }
203
    
204
    public byte[] getBytes() {
205
        return bytes;
206
    }
207
    
208
    public void setObjNum(int objNum, int objGen) {
209
        this.objNum = objNum;
210
        this.objGen = objGen;
211
    }
212
    
213
    int getObjNum() {
214
        return objNum;
215
    }
216
    
217
    int getObjGen() {
218
        return objGen;
219
    }
220
    
221
    public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
222
        byte[] b = PdfReader.getStreamBytesRaw(this);
223
        PdfEncryption crypto = null;
224 1 1. toPdf : negated conditional → NO_COVERAGE
        if (writer != null)
225
            crypto = writer.getEncryption();
226
        PdfObject objLen = get(PdfName.LENGTH);
227
        int nn = b.length;
228 1 1. toPdf : negated conditional → NO_COVERAGE
        if (crypto != null)
229
            nn = crypto.calculateStreamSize(nn);
230 1 1. toPdf : removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE
        put(PdfName.LENGTH, new PdfNumber(nn));
231 1 1. toPdf : removed call to com/lowagie/text/pdf/PRStream::superToPdf → NO_COVERAGE
        superToPdf(writer, os);
232 1 1. toPdf : removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE
        put(PdfName.LENGTH, objLen);
233 1 1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE
        os.write(STARTSTREAM);
234 2 1. toPdf : changed conditional boundary → NO_COVERAGE
2. toPdf : negated conditional → NO_COVERAGE
        if (length > 0) {
235 2 1. toPdf : negated conditional → NO_COVERAGE
2. toPdf : negated conditional → NO_COVERAGE
            if (crypto != null && !crypto.isEmbeddedFilesOnly())
236
                b = crypto.encryptByteArray(b);
237 1 1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE
            os.write(b);
238
        }
239 1 1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE
        os.write(ENDSTREAM);
240
    }
241
}

Mutations

82

1.1
Location :
Killed by : none
negated conditional → NO_COVERAGE

83

1.1
Location :
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::putAll → NO_COVERAGE

85

1.1
Location :
Killed by : none
removed call to java/util/Map::putAll → NO_COVERAGE

113

1.1
Location :
Killed by : none
negated conditional → NO_COVERAGE

118

1.1
Location :
Killed by : none
removed call to java/util/zip/DeflaterOutputStream::write → NO_COVERAGE

119

1.1
Location :
Killed by : none
removed call to java/util/zip/DeflaterOutputStream::close → NO_COVERAGE

120

1.1
Location :
Killed by : none
removed call to java/util/zip/Deflater::end → NO_COVERAGE

126

1.1
Location :
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE

130

1.1
Location :
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::setLength → NO_COVERAGE

143

1.1
Location : setData
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::setData → NO_COVERAGE

157

1.1
Location : setData
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::remove → NO_COVERAGE

159

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

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

164

1.1
Location : setData
Killed by : none
removed call to java/util/zip/DeflaterOutputStream::write → NO_COVERAGE

165

1.1
Location : setData
Killed by : none
removed call to java/util/zip/DeflaterOutputStream::close → NO_COVERAGE

166

1.1
Location : setData
Killed by : none
removed call to java/util/zip/Deflater::end → NO_COVERAGE

173

1.1
Location : setData
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE

177

1.1
Location : setData
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::setLength → NO_COVERAGE

184

1.1
Location : setData
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::setData → NO_COVERAGE

189

1.1
Location : setLength
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE

224

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

228

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

230

1.1
Location : toPdf
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE

231

1.1
Location : toPdf
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::superToPdf → NO_COVERAGE

232

1.1
Location : toPdf
Killed by : none
removed call to com/lowagie/text/pdf/PRStream::put → NO_COVERAGE

233

1.1
Location : toPdf
Killed by : none
removed call to java/io/OutputStream::write → NO_COVERAGE

234

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

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

235

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

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

237

1.1
Location : toPdf
Killed by : none
removed call to java/io/OutputStream::write → NO_COVERAGE

239

1.1
Location : toPdf
Killed by : none
removed call to java/io/OutputStream::write → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2