From 94279c4cc7829fe380dac67ff334afc600616f72 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Fri, 17 Feb 2017 09:53:49 +0000 Subject: [PATCH] bug 59893: close open resources to prevent leaks git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1783362 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/poifs/crypt/TestDecryptor.java | 5 ++++- .../org/apache/poi/poifs/crypt/TestEncryptor.java | 13 +++++++++++-- .../apache/poi/poifs/filesystem/ReaderWriter.java | 4 +++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestDecryptor.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestDecryptor.java index 4ccd0b1aeb..c6c8f959e7 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestDecryptor.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestDecryptor.java @@ -165,11 +165,14 @@ public class TestDecryptor { @Test public void test58616() throws IOException, GeneralSecurityException { - POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream(XSSFTestDataSamples.getSampleFile("58616.xlsx"))); + FileInputStream fis = new FileInputStream(XSSFTestDataSamples.getSampleFile("58616.xlsx")); + POIFSFileSystem pfs = new POIFSFileSystem(fis); EncryptionInfo info = new EncryptionInfo(pfs); Decryptor dec = Decryptor.getInstance(info); //dec.verifyPassword(null); dec.getDataStream(pfs); + pfs.close(); + fis.close(); } @Test diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java index d31a58a334..2e95fa59d3 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java @@ -151,7 +151,8 @@ public class TestEncryptor { ByteArrayOutputStream bos = new ByteArrayOutputStream(); fs.writeFilesystem(bos); - + fs.close(); + nfs = new NPOIFSFileSystem(new ByteArrayInputStream(bos.toByteArray())); infoActual = new EncryptionInfo(nfs.getRoot()); Decryptor decActual = Decryptor.getInstance(infoActual); @@ -297,11 +298,13 @@ public class TestEncryptor { enc.confirmPassword("password"); OutputStream os = enc.getDataStream(fs); pkg.save(os); + os.close(); pkg.revert(); // Save the resulting OLE2 document, and re-open it ByteArrayOutputStream baos = new ByteArrayOutputStream(); fs.writeFilesystem(baos); + fs.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); NPOIFSFileSystem inpFS = new NPOIFSFileSystem(bais); @@ -318,6 +321,9 @@ public class TestEncryptor { assertNotNull(inpPkg.getPackageProperties()); assertNotNull(inpPkg.getPackageProperties().getLanguageProperty()); assertNull(inpPkg.getPackageProperties().getLanguageProperty().getValue()); + + inpPkg.close(); + inpFS.close(); } @Test @@ -340,7 +346,8 @@ public class TestEncryptor { assertTrue(b); // do some strange things with it ;) - XWPFDocument docx = new XWPFDocument(d.getDataStream(fs)); + InputStream docIS = d.getDataStream(fs); + XWPFDocument docx = new XWPFDocument(docIS); docx.getParagraphArray(0).insertNewRun(0).setText("POI was here! All your base are belong to us!"); docx.getParagraphArray(0).insertNewRun(1).addBreak(); @@ -348,6 +355,8 @@ public class TestEncryptor { Encryptor e = encInfo.getEncryptor(); e.confirmPassword("AYBABTU"); docx.write(e.getDataStream(fs)); + docx.close(); + docIS.close(); docx.close(); fs.close(); diff --git a/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java b/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java index b3a328c9ef..2aae3729dc 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/ReaderWriter.java @@ -44,7 +44,7 @@ public class ReaderWriter private final DirectoryEntry root; // keys are DocumentDescriptors, values are byte[]s - private final Map dataMap; + private final Map dataMap; /** * Constructor ReaderWriter @@ -93,6 +93,8 @@ public class ReaderWriter filesystem.writeFilesystem(ostream); ostream.close(); + + filesystem.close(); } } -- 2.39.5