Skip to content

Commit e2386d4

Browse files
committed
PDFBOX-5660: close input, as suggested by Valery Bokov; closes #414
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1931729 13f79535-47bb-0310-9956-ffa450edef68
1 parent 63569c8 commit e2386d4

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

‎pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/image/CCITTFactoryTest.java‎

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,11 @@ void testCreateFromFileLock() throws IOException
238238
String tiffG3Path = "src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/ccittg3.tif";
239239
File copiedTiffFile = new File(TESTRESULTSDIR, "ccittg3.tif");
240240
Files.copy(new File(tiffG3Path).toPath(), copiedTiffFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
241-
PDDocument document = new PDDocument();
242-
CCITTFactory.createFromFile(document, copiedTiffFile);
243-
assertTrue(copiedTiffFile.delete());
241+
try (PDDocument document = new PDDocument())
242+
{
243+
CCITTFactory.createFromFile(document, copiedTiffFile);
244+
assertTrue(copiedTiffFile.delete());
245+
}
244246
}
245247

246248
/**
@@ -254,9 +256,11 @@ void testCreateFromFileNumberLock() throws IOException
254256
String tiffG3Path = "src/test/resources/org/apache/pdfbox/pdmodel/graphics/image/ccittg3.tif";
255257
File copiedTiffFile = new File(TESTRESULTSDIR, "ccittg3n.tif");
256258
Files.copy(new File(tiffG3Path).toPath(), copiedTiffFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
257-
PDDocument document = new PDDocument();
258-
CCITTFactory.createFromFile(document, copiedTiffFile, 0);
259-
assertTrue(copiedTiffFile.delete());
259+
try (PDDocument document = new PDDocument())
260+
{
261+
CCITTFactory.createFromFile(document, copiedTiffFile, 0);
262+
assertTrue(copiedTiffFile.delete());
263+
}
260264
}
261265

262266
/**
@@ -292,23 +296,24 @@ void testFillOrder2() throws IOException, URISyntaxException
292296
{
293297
ba = is.readAllBytes();
294298
}
295-
PDDocument document = new PDDocument();
296-
PDImageXObject ximg = CCITTFactory.createFromByteArray(document, ba);
297-
298-
validate(ximg, 1, 4575, 2232, "tiff", PDDeviceGray.INSTANCE.getName());
299-
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(ba));
300-
checkIdent(bim, ximg.getOpaqueImage(null, 1));
301-
PDPage page = new PDPage(PDRectangle.A4);
302-
document.addPage(page);
303-
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false))
299+
try (PDDocument document = new PDDocument())
304300
{
305-
contentStream.drawImage(ximg, 0, 0, ximg.getWidth() / 8, ximg.getHeight() / 8);
301+
PDImageXObject ximg = CCITTFactory.createFromByteArray(document, ba);
302+
validate(ximg, 1, 4575, 2232, "tiff", PDDeviceGray.INSTANCE.getName());
303+
BufferedImage bim = ImageIO.read(new ByteArrayInputStream(ba));
304+
checkIdent(bim, ximg.getOpaqueImage(null, 1));
305+
PDPage page = new PDPage(PDRectangle.A4);
306+
document.addPage(page);
307+
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false))
308+
{
309+
contentStream.drawImage(ximg, 0, 0, ximg.getWidth() / 8, ximg.getHeight() / 8);
310+
}
311+
document.save(TESTRESULTSDIR + "/Wing.pdf");
306312
}
307-
document.save(TESTRESULTSDIR + "/Wing.pdf");
308-
document.close();
309313

310-
document = Loader.loadPDF(new File(TESTRESULTSDIR, "Wing.pdf"));
311-
assertEquals(1, document.getNumberOfPages());
312-
document.close();
314+
try (PDDocument document = Loader.loadPDF(new File(TESTRESULTSDIR, "Wing.pdf")))
315+
{
316+
assertEquals(1, document.getNumberOfPages());
317+
}
313318
}
314319
}

0 commit comments

Comments
 (0)