Skip to content

Commit 71383ea

Browse files
committed
PDFBOX-5660: close input, as suggested by Valery Bokov; refactor; closes #308
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1929462 13f79535-47bb-0310-9956-ffa450edef68
1 parent 62a3c98 commit 71383ea

File tree

1 file changed

+117
-99
lines changed

1 file changed

+117
-99
lines changed

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java

Lines changed: 117 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.awt.image.BufferedImage;
2020

2121
import java.io.ByteArrayInputStream;
22-
import java.io.ByteArrayOutputStream;
2322
import java.io.File;
2423
import java.io.IOException;
2524
import java.io.InputStream;
@@ -74,13 +73,16 @@ static void setUp()
7473
@Test
7574
void testCreateFromStream() throws IOException
7675
{
77-
PDDocument document = new PDDocument();
78-
InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg");
79-
PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
80-
validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());
76+
try (PDDocument document = new PDDocument();
77+
InputStream is1 = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg");
78+
InputStream is2 = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"))
79+
{
80+
PDImageXObject ximage = JPEGFactory.createFromStream(document, is1);
81+
validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());
8182

82-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgbstream.pdf");
83-
checkJpegStream(TESTRESULTSDIR, "jpegrgbstream.pdf", JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
83+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgbstream.pdf");
84+
checkJpegStream(TESTRESULTSDIR, "jpegrgbstream.pdf", is2);
85+
}
8486
}
8587

8688
/*
@@ -90,13 +92,16 @@ void testCreateFromStream() throws IOException
9092
@Test
9193
void testCreateFromStreamCMYK() throws IOException
9294
{
93-
PDDocument document = new PDDocument();
94-
InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg");
95-
PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
96-
validate(ximage, 8, 343, 287, "jpg", PDDeviceCMYK.INSTANCE.getName());
95+
try (InputStream is1 = JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg");
96+
InputStream is2 = JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg"))
97+
{
98+
PDDocument document = new PDDocument();
99+
PDImageXObject ximage = JPEGFactory.createFromStream(document, is1);
100+
validate(ximage, 8, 343, 287, "jpg", PDDeviceCMYK.INSTANCE.getName());
97101

98-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpegcmykstream.pdf");
99-
checkJpegStream(TESTRESULTSDIR, "jpegcmykstream.pdf", JPEGFactoryTest.class.getResourceAsStream("jpegcmyk.jpg"));
102+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpegcmykstream.pdf");
103+
checkJpegStream(TESTRESULTSDIR, "jpegcmykstream.pdf", is2);
104+
}
100105
}
101106

102107
/**
@@ -106,13 +111,16 @@ void testCreateFromStreamCMYK() throws IOException
106111
@Test
107112
void testCreateFromStream256() throws IOException
108113
{
109-
PDDocument document = new PDDocument();
110-
InputStream stream = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg");
111-
PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
112-
validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName());
114+
try (InputStream is1 = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg");
115+
InputStream is2 = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"))
116+
{
117+
PDDocument document = new PDDocument();
118+
PDImageXObject ximage = JPEGFactory.createFromStream(document, is1);
119+
validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName());
113120

114-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256stream.pdf");
115-
checkJpegStream(TESTRESULTSDIR, "jpeg256stream.pdf", JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"));
121+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256stream.pdf");
122+
checkJpegStream(TESTRESULTSDIR, "jpeg256stream.pdf", is2);
123+
}
116124
}
117125

118126
/**
@@ -122,13 +130,16 @@ void testCreateFromStream256() throws IOException
122130
@Test
123131
void testCreateFromImageRGB() throws IOException
124132
{
125-
PDDocument document = new PDDocument();
126-
BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
127-
assertEquals(3, image.getColorModel().getNumComponents());
128-
PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
129-
validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());
133+
try (InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"))
134+
{
135+
PDDocument document = new PDDocument();
136+
BufferedImage image = ImageIO.read(is);
137+
assertEquals(3, image.getColorModel().getNumComponents());
138+
PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
139+
validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());
130140

131-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgb.pdf");
141+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpegrgb.pdf");
142+
}
132143
}
133144

134145
/**
@@ -138,13 +149,16 @@ void testCreateFromImageRGB() throws IOException
138149
@Test
139150
void testCreateFromImage256() throws IOException
140151
{
141-
PDDocument document = new PDDocument();
142-
BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"));
143-
assertEquals(1, image.getColorModel().getNumComponents());
144-
PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
145-
validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName());
152+
try (InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg256.jpg"))
153+
{
154+
PDDocument document = new PDDocument();
155+
BufferedImage image = ImageIO.read(is);
156+
assertEquals(1, image.getColorModel().getNumComponents());
157+
PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
158+
validate(ximage, 8, 344, 287, "jpg", PDDeviceGray.INSTANCE.getName());
146159

147-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256.pdf");
160+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg256.pdf");
161+
}
148162
}
149163

150164
/**
@@ -154,32 +168,35 @@ void testCreateFromImage256() throws IOException
154168
@Test
155169
void testCreateFromImageINT_ARGB() throws IOException
156170
{
157-
PDDocument document = new PDDocument();
158-
BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
159-
160-
// create an ARGB image
161-
int width = image.getWidth();
162-
int height = image.getHeight();
163-
BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
164-
Graphics ag = argbImage.getGraphics();
165-
ag.drawImage(image, 0, 0, null);
166-
ag.dispose();
167-
168-
for (int x = 0; x < argbImage.getWidth(); ++x)
171+
try (InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"))
169172
{
170-
for (int y = 0; y < argbImage.getHeight(); ++y)
173+
PDDocument document = new PDDocument();
174+
BufferedImage image = ImageIO.read(is);
175+
176+
// create an ARGB image
177+
int width = image.getWidth();
178+
int height = image.getHeight();
179+
BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
180+
Graphics ag = argbImage.getGraphics();
181+
ag.drawImage(image, 0, 0, null);
182+
ag.dispose();
183+
184+
for (int x = 0; x < argbImage.getWidth(); ++x)
171185
{
172-
argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
186+
for (int y = 0; y < argbImage.getHeight(); ++y)
187+
{
188+
argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
189+
}
173190
}
174-
}
175191

176-
PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage);
177-
validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
178-
assertNotNull(ximage.getSoftMask());
179-
validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
180-
assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);
192+
PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage);
193+
validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
194+
assertNotNull(ximage.getSoftMask());
195+
validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
196+
assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);
181197

182-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-intargb.pdf");
198+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-intargb.pdf");
199+
}
183200
}
184201

185202
/**
@@ -189,32 +206,35 @@ void testCreateFromImageINT_ARGB() throws IOException
189206
@Test
190207
void testCreateFromImage4BYTE_ABGR() throws IOException
191208
{
192-
PDDocument document = new PDDocument();
193-
BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
194-
195-
// create an ARGB image
196-
int width = image.getWidth();
197-
int height = image.getHeight();
198-
BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
199-
Graphics ag = argbImage.getGraphics();
200-
ag.drawImage(image, 0, 0, null);
201-
ag.dispose();
202-
203-
for (int x = 0; x < argbImage.getWidth(); ++x)
209+
try (InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"))
204210
{
205-
for (int y = 0; y < argbImage.getHeight(); ++y)
211+
PDDocument document = new PDDocument();
212+
BufferedImage image = ImageIO.read(is);
213+
214+
// create an ARGB image
215+
int width = image.getWidth();
216+
int height = image.getHeight();
217+
BufferedImage argbImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
218+
Graphics ag = argbImage.getGraphics();
219+
ag.drawImage(image, 0, 0, null);
220+
ag.dispose();
221+
222+
for (int x = 0; x < argbImage.getWidth(); ++x)
206223
{
207-
argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
224+
for (int y = 0; y < argbImage.getHeight(); ++y)
225+
{
226+
argbImage.setRGB(x, y, (argbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
227+
}
208228
}
209-
}
210229

211-
PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage);
212-
validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
213-
assertNotNull(ximage.getSoftMask());
214-
validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
215-
assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);
230+
PDImageXObject ximage = JPEGFactory.createFromImage(document, argbImage);
231+
validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
232+
assertNotNull(ximage.getSoftMask());
233+
validate(ximage.getSoftMask(), 8, width, height, "jpg", PDDeviceGray.INSTANCE.getName());
234+
assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);
216235

217-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-4bargb.pdf");
236+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-4bargb.pdf");
237+
}
218238
}
219239

220240
/**
@@ -225,30 +245,33 @@ void testCreateFromImage4BYTE_ABGR() throws IOException
225245
@Test
226246
void testCreateFromImageUSHORT_555_RGB() throws IOException
227247
{
228-
PDDocument document = new PDDocument();
229-
BufferedImage image = ImageIO.read(JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"));
230-
231-
// create an USHORT_555_RGB image
232-
int width = image.getWidth();
233-
int height = image.getHeight();
234-
BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_555_RGB);
235-
Graphics ag = rgbImage.getGraphics();
236-
ag.drawImage(image, 0, 0, null);
237-
ag.dispose();
238-
239-
for (int x = 0; x < rgbImage.getWidth(); ++x)
248+
try (InputStream is = JPEGFactoryTest.class.getResourceAsStream("jpeg.jpg"))
240249
{
241-
for (int y = 0; y < rgbImage.getHeight(); ++y)
250+
PDDocument document = new PDDocument();
251+
BufferedImage image = ImageIO.read(is);
252+
253+
// create an USHORT_555_RGB image
254+
int width = image.getWidth();
255+
int height = image.getHeight();
256+
BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_555_RGB);
257+
Graphics ag = rgbImage.getGraphics();
258+
ag.drawImage(image, 0, 0, null);
259+
ag.dispose();
260+
261+
for (int x = 0; x < rgbImage.getWidth(); ++x)
242262
{
243-
rgbImage.setRGB(x, y, (rgbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
263+
for (int y = 0; y < rgbImage.getHeight(); ++y)
264+
{
265+
rgbImage.setRGB(x, y, (rgbImage.getRGB(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
266+
}
244267
}
245-
}
246268

247-
PDImageXObject ximage = JPEGFactory.createFromImage(document, rgbImage);
248-
validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
249-
assertNull(ximage.getSoftMask());
269+
PDImageXObject ximage = JPEGFactory.createFromImage(document, rgbImage);
270+
validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
271+
assertNull(ximage.getSoftMask());
250272

251-
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-ushort555rgb.pdf");
273+
doWritePDF(document, ximage, TESTRESULTSDIR, "jpeg-ushort555rgb.pdf");
274+
}
252275
}
253276

254277
/**
@@ -274,22 +297,17 @@ void testPDFBox5137() throws IOException
274297

275298
// check whether it is possible to extract the jpeg stream exactly
276299
// as it was passed to createFromStream
277-
private void checkJpegStream(File testResultsDir, String filename, InputStream resourceStream)
300+
private void checkJpegStream(File testResultsDir, String filename, InputStream expected)
278301
throws IOException
279302
{
280303
try (PDDocument doc = Loader.loadPDF(new File(testResultsDir, filename)))
281304
{
282305
PDImageXObject img =
283306
(PDImageXObject) doc.getPage(0).getResources().getXObject(COSName.getPDFName("Im1"));
284-
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
285-
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
286307
try (InputStream dctStream = img.createInputStream(Arrays.asList(COSName.DCT_DECODE.getName())))
287308
{
288-
resourceStream.transferTo(baos1);
289-
dctStream.transferTo(baos2);
309+
assertArrayEquals(expected.readAllBytes(), dctStream.readAllBytes());
290310
}
291-
resourceStream.close();
292-
assertArrayEquals(baos1.toByteArray(), baos2.toByteArray());
293311
}
294312
}
295313
}

0 commit comments

Comments
 (0)