]> source.dussan.org Git - poi.git/commitdiff
use try block to close output streams
authorPJ Fanning <fanningpj@apache.org>
Sat, 20 Nov 2021 12:48:03 +0000 (12:48 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 20 Nov 2021 12:48:03 +0000 (12:48 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895196 13f79535-47bb-0310-9956-ffa450edef68

28 files changed:
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java
poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTheme.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/ThemesTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComments.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFEndnotes.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFooter.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFFootnotes.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFNumbering.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java

index 7de36c588a9181534d4e358b3a073e0a0faf5f2e..d676373cca39b9eaaf151b747fb5dd885052e67d 100644 (file)
@@ -435,10 +435,10 @@ public class POIXMLDocumentPart {
      * <pre>
      * protected void commit() throws IOException {
      *   PackagePart part = getPackagePart();
-     *   OutputStream out = part.getOutputStream();
-     *   XmlObject bean = getXmlBean(); //the "model" which holds changes in memory
-     *   bean.save(out, DEFAULT_XML_OPTIONS);
-     *   out.close();
+     *   try (OutputStream out = part.getOutputStream()) {
+     *     XmlObject bean = getXmlBean(); //the "model" which holds changes in memory
+     *     bean.save(out, DEFAULT_XML_OPTIONS);
+     *   }
      * }
      * </pre>
      *
index d357ab1f7faec469bd21d021948276d512d88b80..2bf840a34f9958031379e10b6dac1b0155f4ddfd 100644 (file)
@@ -73,9 +73,9 @@ public final class PackageHelper {
                 dest.addRelationship(part.getPartName(), rel.getTargetMode(), rel.getRelationshipType());
                 part_tgt = dest.createPart(part.getPartName(), part.getContentType());
 
-                OutputStream out = part_tgt.getOutputStream();
-                IOUtils.copy(part.getInputStream(), out);
-                out.close();
+                try (OutputStream out = part_tgt.getOutputStream()) {
+                    IOUtils.copy(part.getInputStream(), out);
+                }
 
                 if (part.hasRelationships()) {
                     copy(pkg, part, dest, part_tgt);
@@ -113,9 +113,12 @@ public final class PackageHelper {
             PackagePart dest;
             if(!tgt.containPart(p.getPartName())){
                 dest = tgt.createPart(p.getPartName(), p.getContentType());
-                OutputStream out = dest.getOutputStream();
-                IOUtils.copy(p.getInputStream(), out);
-                out.close();
+                try (
+                        InputStream in = p.getInputStream();
+                        OutputStream out = dest.getOutputStream()
+                ) {
+                    IOUtils.copy(in, out);
+                }
                 copy(pkg, p, tgt, dest);
             }
         }
index d28452d4255aa57caa3212781b04531166515354..6cd0a02815f978916c1e62e8c194a7b3aa35d112 100644 (file)
@@ -1562,9 +1562,9 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
      *      "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
      *      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
      *
-     *  FileOutputStream out = new FileOutputStream("workbook.xlsx");
-     *  pkg.save(out);
-     *  out.close();
+     *  try (FileOutputStream out = new FileOutputStream("workbook.xlsx")) {
+     *    pkg.save(out);
+     *  }
      *
      * }</pre>
      *
index 7347a785c2d0da7c2b178f7a26640cfac9d01722..c0b05b921121d8e351a5dd75c5a4b03eecb07eb0 100644 (file)
@@ -197,9 +197,9 @@ public class XMLSlideShow extends POIXMLDocument
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        _presentation.save(out, DEFAULT_XML_OPTIONS);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            _presentation.save(out, DEFAULT_XML_OPTIONS);
+        }
     }
 
     /**
index 47be24663afabe85cdd1c0cef82096816472694a..c34d8f3800cbada427d24d2a513b09ba61c203bf 100644 (file)
@@ -412,9 +412,9 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
         }
 
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        getXmlObject().save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            getXmlObject().save(out, xmlOptions);
+        }
     }
 
     /**
@@ -671,9 +671,9 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
     /**
      * Import a package part into this sheet.
      */
-    void importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
+    void importPart(PackageRelationship srcRel, PackagePart srcPart) {
         PackagePart destPP = getPackagePart();
-        PackagePartName srcPPName = srcPafrt.getPartName();
+        PackagePartName srcPPName = srcPart.getPartName();
 
         OPCPackage pkg = destPP.getPackage();
         if(pkg.containPart(srcPPName)){
@@ -683,13 +683,12 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
 
         destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType());
 
-        PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType());
-        try {
-            OutputStream out = part.getOutputStream();
-            InputStream is = srcPafrt.getInputStream();
+        PackagePart part = pkg.createPart(srcPPName, srcPart.getContentType());
+        try(
+                OutputStream out = part.getOutputStream();
+                InputStream is = srcPart.getInputStream();
+        ) {
             IOUtils.copy(is, out);
-            is.close();
-            out.close();
         } catch (IOException e){
             throw new POIXMLException(e);
         }
index f033792eb4d083a6f4c96767c03f5481a99aa9aa..556ad6e6e764893f9f8a63004ced2b8bc69d33e9 100644 (file)
@@ -140,9 +140,9 @@ public class XSLFTheme extends POIXMLDocumentPart {
             new QName(XSLFRelation.NS_DRAWINGML, "theme"));
 
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        getXmlObject().save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            getXmlObject().save(out, xmlOptions);
+        }
     }
 
     /**
index d46a6af3ee24d75d0fb503aff93083b10e04e8ee..ad962321f1ebc48bba32b3ad2a6e879449b7e4eb 100644 (file)
@@ -66,9 +66,9 @@ public class CalculationChain extends POIXMLDocumentPart {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
 
index 1ad6d0f7e2a0f62ea55f3d3552243f352506e472..e9fcc043ce3fecd31f8b4b6fcedcc0771bd4cc17 100644 (file)
@@ -95,9 +95,9 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
     /**
index db970d3e9f6619fe2c67c1538f58a1fb382a8351..9d334b5dee0ffe78240958762e48c947b4616bee 100644 (file)
@@ -30,6 +30,8 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
 import org.apache.poi.openxml4j.opc.TargetMode;
 import org.apache.poi.ss.usermodel.Name;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.Removal;
 import org.apache.xmlbeans.XmlException;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalDefinedName;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalLink;
@@ -74,15 +76,19 @@ public class ExternalLinksTable extends POIXMLDocumentPart {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
     /**
      * Returns the underlying xmlbeans object for the external
-     *  link table
+     *  link table. Internal use only. Not currently used internally.
+     * @deprecated will be removed because we don't want to expose this (future implementations may not be
+     *              XMLBeans based)
      */
+    @Internal
+    @Removal(version = "6.0.0")
     public CTExternalLink getCTExternalLink(){
         return link;
     }
index c210181e9ae59fd3299fcb6e5e53f7fae5f277bd..b70b4dbc684d97add3d1c835010b9094fbc57cae 100644 (file)
@@ -151,9 +151,9 @@ public class MapInfo extends POIXMLDocumentPart {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
 }
index 445865bcaf1f3e3d72c788d388f5b06250e709da..8dad4297070b030049b724ee8861a19b30afe87f 100644 (file)
@@ -81,9 +81,9 @@ public class SingleXmlCells extends POIXMLDocumentPart {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
     public CTSingleXmlCells getCTSingleXMLCells(){
index 60b1a9ce426944e94418654a446554392447d81e..db9565347b5d8c5e56e81efae31bfccaaff3ebf5 100644 (file)
@@ -726,9 +726,9 @@ public class StylesTable extends POIXMLDocumentPart implements Styles {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
     private void initialize() {
index c5ec2bfd9be16af25356c3ce937ded5796601d92..28d5a0633ecfed81051fe9c0a330d32827fb35b7 100644 (file)
@@ -183,8 +183,8 @@ public class ThemesTable extends POIXMLDocumentPart implements Themes {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 }
index ce57c967401fa453d20edaa5decfc052afb203c6..eed308d42226e4f07969a2583a0cde269960e1a1 100644 (file)
@@ -152,9 +152,9 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
             .setSaveSyntheticDocumentElement(new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr"));
 
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        drawing.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            drawing.save(out, xmlOptions);
+        }
     }
 
     @Override
index d55470c335f2b5a7e8404dff469b2801ca945355..0dabd263a1b68d705c8b3c1395162ebcabcdd5ba 100644 (file)
@@ -100,13 +100,13 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
-        //Sets the pivotCacheDefinition tag
-        xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotCacheDefinition.type.getName().
-                getNamespaceURI(), "pivotCacheDefinition"));
-        ctPivotCacheDefinition.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+            //Sets the pivotCacheDefinition tag
+            xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotCacheDefinition.type.getName().
+                    getNamespaceURI(), "pivotCacheDefinition"));
+            ctPivotCacheDefinition.save(out, xmlOptions);
+        }
     }
 
     /**
index 1172007901ccfbede78eeb84468f8f72ad5c20a3..d55b22479d955f124609bac278f8d27b94a87b9c 100644 (file)
@@ -77,12 +77,12 @@ public class XSSFPivotCacheRecords extends POIXMLDocumentPart {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
-        //Sets the pivotCacheDefinition tag
-        xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotCacheRecords.type.getName().
-                getNamespaceURI(), "pivotCacheRecords"));
-        ctPivotCacheRecords.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+            //Sets the pivotCacheDefinition tag
+            xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotCacheRecords.type.getName().
+                    getNamespaceURI(), "pivotCacheRecords"));
+            ctPivotCacheRecords.save(out, xmlOptions);
+        }
     }
 }
\ No newline at end of file
index de45c7d9e55260475516151921a57fbfb947501d..60ca7d0cd21261b124315790637ec75c03c3d877 100644 (file)
@@ -180,9 +180,9 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTPivotTableDefinition.type.getName().
                 getNamespaceURI(), "pivotTableDefinition"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        pivotTableDefinition.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            pivotTableDefinition.save(out, xmlOptions);
+        }
     }
 
     /**
index 7a004cc8e03d4e3cf167397a70182f97a688cd7a..2f92f5ce5527b36e094d622b60eaf6626166a040 100644 (file)
@@ -3594,9 +3594,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet  {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        write(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            write(out);
+        }
     }
 
     protected void write(OutputStream out) throws IOException {
index fbba2f697a59b0064fdfd0984a76af368d5f68e5..7cf2472ca464784b497ddacc2fce9837d3f8c882 100644 (file)
@@ -121,9 +121,9 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
     @Override
     protected void commit() throws IOException {
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        writeTo(out);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            writeTo(out);
+        }
     }
 
     /**
index 2d5fea4fbbf9c37d3a3bf0f54ff1250d49be08d5..e6b62b8f6ecda52a0c775469785eb618e0cf2f07 100644 (file)
@@ -164,9 +164,9 @@ public class XWPFComments extends POIXMLDocumentPart {
         xmlOptions.setSaveSyntheticDocumentElement(new QName(
                 CTComments.type.getName().getNamespaceURI(), "comments"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctComments.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctComments.save(out, xmlOptions);
+        }
     }
 
     public List<XWPFPictureData> getAllPictures() {
index eeed142563c782d8ea52dcb8ca20d4a1bdc7aa6e..0e7116bb8eb7ef255056cc37ea4706c841d31b3d 100644 (file)
@@ -816,9 +816,9 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document"));
 
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctDocument.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctDocument.save(out, xmlOptions);
+        }
     }
 
     /**
index 1ac9a05d9851db23e7c85582920b7474d46a3f6b..add139ebbe4bb7a7abaadfeaabee375cf578b1a1 100644 (file)
@@ -126,9 +126,9 @@ public class XWPFEndnotes extends XWPFAbstractFootnotesEndnotes {
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTEndnotes.type.getName().getNamespaceURI(), "endnotes"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctEndnotes.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctEndnotes.save(out, xmlOptions);
+        }
     }
 
     /**
index c6a5cf22015464683e9fc72504dfb27fc2543152..c86ce8b74d2467f5a872612256a041c66251b916 100644 (file)
@@ -81,9 +81,9 @@ public class XWPFFooter extends XWPFHeaderFooter {
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "ftr"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        super._getHdrFtr().save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            super._getHdrFtr().save(out, xmlOptions);
+        }
     }
 
     @Override
index 278e16da1a106b1d5e049e24a7d0c4cf1e0625b5..f9bdfb7ad84ee0f7eb16eee1c3560628f12e74f2 100644 (file)
@@ -127,9 +127,9 @@ public class XWPFFootnotes extends XWPFAbstractFootnotesEndnotes {
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTFootnotes.type.getName().getNamespaceURI(), "footnotes"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctFootnotes.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctFootnotes.save(out, xmlOptions);
+        }
     }
 
     /**
index 5d7bc2885ac6cd88f094d75ae2089b96668fe7d5..eaa466adab1ef00713b7c9dad7f1fae93d1ffa96 100644 (file)
@@ -79,9 +79,9 @@ public class XWPFHeader extends XWPFHeaderFooter {
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "hdr"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        super._getHdrFtr().save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            super._getHdrFtr().save(out, xmlOptions);
+        }
     }
 
     /**
index 53ff91fe77a6bfa92d27d16b4435839019c67056..2ff4f4c5eb7aa3b74396218c77a480fb6d036088 100644 (file)
@@ -97,9 +97,9 @@ public class XWPFNumbering extends POIXMLDocumentPart {
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "numbering"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctNumbering.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctNumbering.save(out, xmlOptions);
+        }
     }
 
 
index 0ed2adbf9db2a2316ff8410b2681b9519270bcdb..03b1090f73f8aa8705cf572433eb4c6779b2a21c 100644 (file)
@@ -396,9 +396,9 @@ public class XWPFSettings extends POIXMLDocumentPart {
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTSettings.type.getName().getNamespaceURI(), "settings"));
 
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctSettings.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctSettings.save(out, xmlOptions);
+        }
     }
 
     private CTDocProtect safeGetDocumentProtection() {
index 93b05ab901c683aafef7fe342b4698b99641d195..7038315ad38657e21a97e46d17266cbe5f190f5f 100644 (file)
@@ -97,9 +97,9 @@ public class XWPFStyles extends POIXMLDocumentPart {
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
         xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
         PackagePart part = getPackagePart();
-        OutputStream out = part.getOutputStream();
-        ctStyles.save(out, xmlOptions);
-        out.close();
+        try (OutputStream out = part.getOutputStream()) {
+            ctStyles.save(out, xmlOptions);
+        }
     }
 
     protected void ensureDocDefaults() {