Skip to content

Commit b003f09

Browse files
committed
Move all tasks with Inflater and InflaterOutputStream to io.utils.FilterUtil.
1 parent 6425ded commit b003f09

File tree

7 files changed

+91
-43
lines changed

7 files changed

+91
-43
lines changed

io/src/main/java/com/itextpdf/io/codec/PngWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.itextpdf.io.codec;
22

33
import com.itextpdf.io.source.ByteUtils;
4+
import com.itextpdf.io.source.DeflaterOutputStream;
45

56
import java.io.ByteArrayOutputStream;
67
import java.io.IOException;
78
import java.io.OutputStream;
8-
import java.util.zip.DeflaterOutputStream;
99

1010
/**
1111
* Writes a PNG image.

io/src/main/java/com/itextpdf/io/image/PngImageHelper.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import java.io.InputStream;
1414
import java.util.HashMap;
1515
import java.util.Map;
16-
import java.util.zip.Inflater;
17-
import java.util.zip.InflaterInputStream;
1816

1917
public class PngImageHelper {
2018

@@ -529,8 +527,7 @@ private static void decodeIdat(PngParameters png) {
529527
else if (png.genBWMask)
530528
png.smask = new byte[(png.width + 7) / 8 * png.height];
531529
ByteArrayInputStream bai = new ByteArrayInputStream(png.idat.getBuf(), 0, png.idat.size());
532-
InputStream infStream = new InflaterInputStream(bai, new Inflater());
533-
png.dataStream = new DataInputStream(infStream);
530+
png.dataStream = new DataInputStream(FilterUtil.getInflaterInputStream(bai));
534531

535532
if (png.interlaceMethod != 1) {
536533
decodePass(0, 0, 1, 1, png.width, png.height, png);

io/src/main/java/com/itextpdf/io/image/TiffImageHelper.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
import com.itextpdf.io.color.IccProfile;
1111
import com.itextpdf.io.font.PdfEncodings;
1212
import com.itextpdf.io.source.ByteArrayOutputStream;
13+
import com.itextpdf.io.source.DeflaterOutputStream;
1314
import com.itextpdf.io.source.RandomAccessFileOrArray;
1415
import com.itextpdf.io.source.RandomAccessSource;
1516
import com.itextpdf.io.source.RandomAccessSourceFactory;
17+
import com.itextpdf.io.util.FilterUtil;
1618

1719
import java.io.InputStream;
1820
import java.util.HashMap;
1921
import java.util.Map;
20-
import java.util.zip.DataFormatException;
21-
import java.util.zip.DeflaterOutputStream;
22-
import java.util.zip.Inflater;
2322

2423
public class TiffImageHelper {
2524

@@ -452,7 +451,7 @@ else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIEN
452451
switch (compression) {
453452
case TIFFConstants.COMPRESSION_DEFLATE:
454453
case TIFFConstants.COMPRESSION_ADOBE_DEFLATE:
455-
inflate(im, outBuf);
454+
FilterUtil.inflateData(im, outBuf);
456455
applyPredictor(outBuf, predictor, w, height, samplePerPixel);
457456
break;
458457
case TIFFConstants.COMPRESSION_NONE:
@@ -633,16 +632,6 @@ private static void decodePackbits(byte data[], byte[] dst) {
633632
}
634633
}
635634

636-
private static void inflate(byte[] deflated, byte[] inflated) {
637-
Inflater inflater = new Inflater();
638-
inflater.setInput(deflated);
639-
try {
640-
inflater.inflate(inflated);
641-
} catch (DataFormatException dfe) {
642-
throw new IOException(IOException.CannotInflateTiffImage);
643-
}
644-
}
645-
646635
private static void applyPredictor(byte[] uncompData, int predictor, int w, int h, int samplesPerPixel) {
647636
if (predictor != 2)
648637
return;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.itextpdf.io.source;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.util.zip.Deflater;
6+
7+
public class DeflaterOutputStream extends java.util.zip.DeflaterOutputStream {
8+
9+
public DeflaterOutputStream(java.io.OutputStream out, int level, int size, boolean syncFlush) {
10+
super(out, new Deflater(level), size, syncFlush);
11+
}
12+
13+
public DeflaterOutputStream(OutputStream out, int level, int size) {
14+
this(out, level, size, false);
15+
}
16+
17+
public DeflaterOutputStream(OutputStream out, int level, boolean syncFlush) {
18+
this(out, level, 512, syncFlush);
19+
}
20+
21+
public DeflaterOutputStream(OutputStream out, int level) {
22+
this(out, level, false);
23+
}
24+
25+
public DeflaterOutputStream(OutputStream out, boolean syncFlush) {
26+
this(out, -1, syncFlush);
27+
}
28+
29+
public DeflaterOutputStream(OutputStream out) {
30+
this(out, false);
31+
}
32+
33+
@Override
34+
public void close() throws IOException {
35+
finish();
36+
super.close();
37+
}
38+
39+
@Override
40+
public void finish() throws IOException {
41+
super.finish();
42+
def.end();
43+
}
44+
}

io/src/main/java/com/itextpdf/io/util/FilterUtil.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.itextpdf.io.util;
22

3+
import com.itextpdf.io.IOException;
4+
import com.itextpdf.io.codec.Base64;
5+
36
import java.io.ByteArrayInputStream;
47
import java.io.ByteArrayOutputStream;
8+
import java.io.InputStream;
9+
import java.util.zip.DataFormatException;
10+
import java.util.zip.Inflater;
511
import java.util.zip.InflaterInputStream;
612

713
public final class FilterUtil {
@@ -47,4 +53,24 @@ public static byte[] flateDecode(byte[] input) {
4753
return flateDecode(input, false);
4854
return b;
4955
}
56+
57+
/**
58+
* This method provides support for general purpose decompression using the
59+
* popular ZLIB compression library.
60+
* @param deflated the input data bytes
61+
* @param inflated the buffer for the uncompressed data
62+
*/
63+
public static void inflateData(byte[] deflated, byte[] inflated) {
64+
Inflater inflater = new Inflater();
65+
inflater.setInput(deflated);
66+
try {
67+
inflater.inflate(inflated);
68+
} catch (DataFormatException dfe) {
69+
throw new IOException(IOException.CannotInflateTiffImage);
70+
}
71+
}
72+
73+
public static InputStream getInflaterInputStream(InputStream input) {
74+
return new InflaterInputStream(input, new Inflater());
75+
}
5076
}

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfOutputStream.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.itextpdf.io.source.ByteArrayOutputStream;
44
import com.itextpdf.io.source.ByteUtils;
5+
import com.itextpdf.io.source.DeflaterOutputStream;
56
import com.itextpdf.io.source.OutputStream;
67
import com.itextpdf.kernel.PdfException;
78
import com.itextpdf.kernel.crypto.OutputStreamEncryption;
@@ -11,8 +12,6 @@
1112
import java.io.Serializable;
1213
import java.security.cert.Certificate;
1314
import java.util.Map;
14-
import java.util.zip.Deflater;
15-
import java.util.zip.DeflaterOutputStream;
1615

1716
public class PdfOutputStream extends OutputStream<PdfOutputStream> implements Serializable{
1817

@@ -98,19 +97,19 @@ public class PdfOutputStream extends OutputStream<PdfOutputStream> implements Se
9897
/**
9998
* A possible compression level.
10099
*/
101-
public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION;
100+
public static final int DEFAULT_COMPRESSION = java.util.zip.Deflater.DEFAULT_COMPRESSION;
102101
/**
103102
* A possible compression level.
104103
*/
105-
public static final int NO_COMPRESSION = Deflater.NO_COMPRESSION;
104+
public static final int NO_COMPRESSION = java.util.zip.Deflater.NO_COMPRESSION;
106105
/**
107106
* A possible compression level.
108107
*/
109-
public static final int BEST_SPEED = Deflater.BEST_SPEED;
108+
public static final int BEST_SPEED = java.util.zip.Deflater.BEST_SPEED;
110109
/**
111110
* A possible compression level.
112111
*/
113-
public static final int BEST_COMPRESSION = Deflater.BEST_COMPRESSION;
112+
public static final int BEST_COMPRESSION = java.util.zip.Deflater.BEST_COMPRESSION;
114113

115114
private static final byte[] stream = ByteUtils.getIsoBytes("stream\n");
116115
private static final byte[] endstream = ByteUtils.getIsoBytes("\nendstream");
@@ -352,11 +351,9 @@ protected void write(PdfStream pdfStream) {
352351
if (crypto != null && !crypto.isEmbeddedFilesOnly()) {
353352
fout = ose = crypto.getEncryptionStream(fout);
354353
}
355-
Deflater deflater = null;
356354
if (toCompress && (allowCompression || userDefinedCompression)) {
357355
updateCompressionFilter(pdfStream);
358-
deflater = new Deflater(pdfStream.getCompressionLevel());
359-
fout = def = new DeflaterOutputStream(fout, deflater, 0x8000);
356+
fout = def = new DeflaterOutputStream(fout, pdfStream.getCompressionLevel(), 0x8000);
360357
}
361358
write((PdfDictionary) pdfStream);
362359
writeBytes(PdfOutputStream.stream);
@@ -370,7 +367,6 @@ protected void write(PdfStream pdfStream) {
370367
}
371368
if (def != null) {
372369
def.finish();
373-
deflater.end();
374370
}
375371
if (ose != null) {
376372
ose.finish();
@@ -398,8 +394,7 @@ protected void write(PdfStream pdfStream) {
398394
if (toCompress && !containsFlateFilter(pdfStream) && (allowCompression || userDefinedCompression)) { // compress
399395
updateCompressionFilter(pdfStream);
400396
byteArrayStream = new ByteArrayOutputStream();
401-
Deflater deflater = new Deflater(pdfStream.getCompressionLevel());
402-
DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream, deflater);
397+
DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream, pdfStream.getCompressionLevel());
403398
if (pdfStream instanceof PdfObjectStream) {
404399
PdfObjectStream objectStream = (PdfObjectStream) pdfStream;
405400
((ByteArrayOutputStream) objectStream.getIndexStream().getOutputStream()).writeTo(zip);
@@ -410,7 +405,6 @@ protected void write(PdfStream pdfStream) {
410405
}
411406

412407
zip.close();
413-
deflater.end();
414408
} else {
415409
if (pdfStream instanceof PdfObjectStream) {
416410
PdfObjectStream objectStream = (PdfObjectStream) pdfStream;

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfDocumentTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.itextpdf.kernel.pdf;
22

33
import com.itextpdf.io.image.ImageFactory;
4+
import com.itextpdf.io.source.DeflaterOutputStream;
45
import com.itextpdf.kernel.pdf.navigation.PdfDestination;
56
import com.itextpdf.kernel.utils.CompareTool;
67
import com.itextpdf.test.annotations.type.IntegrationTest;
78
import com.itextpdf.test.ExtendedITextTest;
89

9-
10-
import java.io.*;
11-
import java.util.zip.Deflater;
12-
import java.util.zip.DeflaterOutputStream;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.FileInputStream;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
1314

1415
import org.junit.Assert;
1516
import org.junit.BeforeClass;
@@ -252,29 +253,26 @@ public void addUnusedStreamObjectsTest() throws IOException, InterruptedExceptio
252253

253254
@Test
254255
public void testImageCompressLevel() throws IOException {
255-
byte[] b = ImageFactory.getImage(sourceFolder+"berlin2013.jpg").getData();
256+
byte[] b = ImageFactory.getImage(sourceFolder + "berlin2013.jpg").getData();
256257
com.itextpdf.io.source.ByteArrayOutputStream image = new com.itextpdf.io.source.ByteArrayOutputStream();
257258
image.assignBytes(b, b.length);
258259

259260
ByteArrayOutputStream byteArrayStream1 = new com.itextpdf.io.source.ByteArrayOutputStream();
260-
Deflater deflater = new Deflater(9);
261-
DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream1, deflater);
261+
DeflaterOutputStream zip = new DeflaterOutputStream(byteArrayStream1, 9);
262262
image.writeTo(zip);
263263
zip.close();
264264

265265
ByteArrayOutputStream byteArrayStream2 = new com.itextpdf.io.source.ByteArrayOutputStream();
266-
Deflater deflater2 = new Deflater(-1);
267-
DeflaterOutputStream zip2 = new DeflaterOutputStream(byteArrayStream2, deflater2);
266+
DeflaterOutputStream zip2 = new DeflaterOutputStream(byteArrayStream2, -1);
268267
image.writeTo(zip2);
269268
zip2.close();
270-
deflater2.end();
271269

272270
Assert.assertTrue(byteArrayStream1.size() == byteArrayStream2.size());
273271
}
274272

275273
@Test
276274
public void testFreeReference() throws IOException, InterruptedException {
277-
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder+ "baseFreeReference.pdf"), new PdfWriter(destinationFolder + "freeReference.pdf"));
275+
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "baseFreeReference.pdf"), new PdfWriter(destinationFolder + "freeReference.pdf"));
278276
pdfDocument.getWriter().setFullCompression(false);
279277
pdfDocument.getPage(1).getResources().getPdfObject().getAsArray(new PdfName("d")).get(0).getIndirectReference().setFree();
280278
PdfStream pdfStream = new PdfStream();

0 commit comments

Comments
 (0)