Skip to content

Commit f4f927d

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

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/common/TestEmbeddedFiles.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@ void testNullEmbeddedFile() throws IOException
4444
{
4545
PDEmbeddedFile embeddedFile = null;
4646
boolean ok = false;
47-
try
47+
try (PDDocument doc = Loader
48+
.loadPDF(RandomAccessReadBuffer.createBufferFromStream(TestEmbeddedFiles.class
49+
.getResourceAsStream("null_PDComplexFileSpecification.pdf"))))
4850
{
49-
PDDocument doc = Loader
50-
.loadPDF(RandomAccessReadBuffer.createBufferFromStream(TestEmbeddedFiles.class
51-
.getResourceAsStream("null_PDComplexFileSpecification.pdf")));
52-
5351
PDDocumentCatalog catalog = doc.getDocumentCatalog();
5452
PDDocumentNameDictionary names = catalog.getNames();
5553
assertEquals(2, names.getEmbeddedFiles().getNames().size(), "expected two files");
@@ -87,34 +85,34 @@ void testOSSpecificAttachments() throws IOException
8785
PDEmbeddedFile dosFile = null;
8886
PDEmbeddedFile unixFile = null;
8987

90-
PDDocument doc = Loader.loadPDF(RandomAccessReadBuffer.createBufferFromStream(
91-
TestEmbeddedFiles.class.getResourceAsStream("testPDF_multiFormatEmbFiles.pdf")));
92-
93-
PDDocumentCatalog catalog = doc.getDocumentCatalog();
94-
PDDocumentNameDictionary names = catalog.getNames();
95-
PDEmbeddedFilesNameTreeNode treeNode = names.getEmbeddedFiles();
96-
List<PDNameTreeNode<PDComplexFileSpecification>> kids = treeNode.getKids();
97-
for (PDNameTreeNode<PDComplexFileSpecification> kid : kids)
88+
try (PDDocument doc = Loader.loadPDF(RandomAccessReadBuffer.createBufferFromStream(
89+
TestEmbeddedFiles.class.getResourceAsStream("testPDF_multiFormatEmbFiles.pdf"))))
9890
{
99-
Map<String, PDComplexFileSpecification> tmpNames = kid.getNames();
100-
COSObjectable obj = tmpNames.get("My first attachment");
91+
PDDocumentCatalog catalog = doc.getDocumentCatalog();
92+
PDDocumentNameDictionary names = catalog.getNames();
93+
PDEmbeddedFilesNameTreeNode treeNode = names.getEmbeddedFiles();
94+
List<PDNameTreeNode<PDComplexFileSpecification>> kids = treeNode.getKids();
95+
for (PDNameTreeNode<PDComplexFileSpecification> kid : kids)
96+
{
97+
Map<String, PDComplexFileSpecification> tmpNames = kid.getNames();
98+
COSObjectable obj = tmpNames.get("My first attachment");
99+
100+
PDComplexFileSpecification spec = (PDComplexFileSpecification) obj;
101+
nonOSFile = spec.getEmbeddedFile();
102+
macFile = spec.getEmbeddedFileMac();
103+
dosFile = spec.getEmbeddedFileDos();
104+
unixFile = spec.getEmbeddedFileUnix();
105+
}
101106

102-
PDComplexFileSpecification spec = (PDComplexFileSpecification) obj;
103-
nonOSFile = spec.getEmbeddedFile();
104-
macFile = spec.getEmbeddedFileMac();
105-
dosFile = spec.getEmbeddedFileDos();
106-
unixFile = spec.getEmbeddedFileUnix();
107+
assertTrue(byteArrayContainsLC("non os specific", nonOSFile.toByteArray(), "ISO-8859-1"),
108+
"non os specific");
109+
assertTrue(byteArrayContainsLC("mac embedded", macFile.toByteArray(), "ISO-8859-1"), "mac");
110+
111+
assertTrue(byteArrayContainsLC("dos embedded", dosFile.toByteArray(), "ISO-8859-1"), "dos");
112+
113+
assertTrue(byteArrayContainsLC("unix embedded", unixFile.toByteArray(), "ISO-8859-1"),
114+
"unix");
107115
}
108-
109-
assertTrue(byteArrayContainsLC("non os specific", nonOSFile.toByteArray(), "ISO-8859-1"),
110-
"non os specific");
111-
assertTrue(byteArrayContainsLC("mac embedded", macFile.toByteArray(), "ISO-8859-1"), "mac");
112-
113-
assertTrue(byteArrayContainsLC("dos embedded", dosFile.toByteArray(), "ISO-8859-1"), "dos");
114-
115-
assertTrue(byteArrayContainsLC("unix embedded", unixFile.toByteArray(), "ISO-8859-1"),
116-
"unix");
117-
118116
}
119117

120118
private boolean byteArrayContainsLC(String target, byte[] bytes, String encoding)

0 commit comments

Comments
 (0)