]> source.dussan.org Git - poi.git/commitdiff
close OPCPackage if there is an exception
authorPJ Fanning <fanningpj@apache.org>
Tue, 7 Jan 2020 18:46:11 +0000 (18:46 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 7 Jan 2020 18:46:11 +0000 (18:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872453 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java

index c57db95506e6440c3525d3c7c2d704d041cfc663..38fe4e4df5b0a8b0a8cb2bed19725209cc6ae989 100644 (file)
@@ -301,7 +301,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
      *       OPCPackage pkg = OPCPackage.open(path);
      *       XSSFWorkbook wb = new XSSFWorkbook(pkg);
      *       // work with the wb object
-     *       ......
+     *       ......XWPFDocument
      *       pkg.close(); // gracefully closes the underlying zip file
      *   </code></pre>
      */
index 5313ea735a9a2c5b55682c999a8697c9322e59d2..4c9796871d694df4c61829455a8b535c6b07dfd8 100644 (file)
@@ -154,8 +154,9 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
      * Create a new WordProcessingML package and setup the default minimal content
      */
     protected static OPCPackage newPackage() {
+        OPCPackage pkg = null;
         try {
-            OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());    // NOSONAR - we do not want to close this here
+            pkg = OPCPackage.create(new ByteArrayOutputStream());    // NOSONAR - we do not want to close this here
             // Main part
             PackagePartName corePartName = PackagingURIHelper.createPartName(XWPFRelation.DOCUMENT.getDefaultFileName());
             // Create main part relationship
@@ -167,6 +168,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
 
             return pkg;
         } catch (Exception e) {
+            IOUtils.closeQuietly(pkg);
             throw new POIXMLException(e);
         }
     }