]> source.dussan.org Git - poi.git/commitdiff
close cursors in finally blocks
authorPJ Fanning <fanningpj@apache.org>
Tue, 28 Dec 2021 10:51:24 +0000 (10:51 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 28 Dec 2021 10:51:24 +0000 (10:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896472 13f79535-47bb-0310-9956-ffa450edef68

poi-examples/src/main/java/org/apache/poi/examples/ss/LoadEmbedded.java
poi-examples/src/main/java/org/apache/poi/examples/xslf/AddVideoToPptx.java.txt
poi-examples/src/main/java/org/apache/poi/examples/xslf/LinkVideoToPptx.java

index 3c52ecf2530e4cc09752914fd80279d3a2af6905..68ea9229ef5b301e38b3de57a0a018cfd0d364bd 100644 (file)
@@ -101,33 +101,46 @@ public final class LoadEmbedded {
            String contentType = pPart.getContentType();
            if (contentType.equals("application/vnd.ms-excel")) {
                // Excel Workbook - either binary or OpenXML
-               HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
-               embeddedWorkbook.close();
+               try (InputStream stream = pPart.getInputStream()) {
+                   HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(stream);
+                   embeddedWorkbook.close();
+               }
            } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
                // Excel Workbook - OpenXML file format
-               XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
-               embeddedWorkbook.close();
+               try (InputStream stream = pPart.getInputStream()) {
+                   XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(stream);
+                   embeddedWorkbook.close();
+               }
            } else if (contentType.equals("application/msword")) {
                // Word Document - binary (OLE2CDF) file format
-               HWPFDocument document = new HWPFDocument(pPart.getInputStream());
-               document.close();
+               try (InputStream stream = pPart.getInputStream()) {
+                   HWPFDocument document = new HWPFDocument(stream);
+                   document.close();
+               }
            } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
                // Word Document - OpenXML file format
-               XWPFDocument document = new XWPFDocument(pPart.getInputStream());
-               document.close();
+               try (InputStream stream = pPart.getInputStream()) {
+                   XWPFDocument document = new XWPFDocument(stream);
+                   document.close();
+               }
            } else if (contentType.equals("application/vnd.ms-powerpoint")) {
                // PowerPoint Document - binary file format
-               HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
-               slideShow.close();
+               try (InputStream stream = pPart.getInputStream()) {
+                   HSLFSlideShow slideShow = new HSLFSlideShow(stream);
+                   slideShow.close();
+               }
            } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
                // PowerPoint Document - OpenXML file format
-               XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
-               slideShow.close();
+               try (InputStream stream = pPart.getInputStream()) {
+                   XMLSlideShow slideShow = new XMLSlideShow(stream);
+                   slideShow.close();
+               }
            } else {
                // Any other type of embedded object.
                System.out.println("Unknown Embedded Document: " + contentType);
-               InputStream inputStream = pPart.getInputStream();
-               inputStream.close();
+               try (InputStream inputStream = pPart.getInputStream()) {
+
+               }
            }
        }
    }
index 2d32c4d81bddf765fbd0f8417cb9238221bfc3db..835fe63d6da6cd7031cd7236be0dfb16ac404b4c 100644 (file)
@@ -166,14 +166,16 @@ public class AddVideoToPptx {
         ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");\r
         String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";\r
         XmlCursor cur = ext.newCursor();\r
-        cur.toEndToken();\r
-        cur.beginElement(new QName(p14Ns, "media", "p14"));\r
-        cur.insertNamespace("p14", p14Ns);\r
-        cur.insertAttributeWithValue(new QName(STRelationshipId.type.getName().getNamespaceURI(), "embed"), prsEmbed1.getId());\r
-        cur.beginElement(new QName(p14Ns, "trim", "p14"));\r
-        cur.insertAttributeWithValue("st", df_time.format(seconds*1000.0));\r
-        cur.dispose();\r
-\r
+        try {\r
+            cur.toEndToken();\r
+            cur.beginElement(new QName(p14Ns, "media", "p14"));\r
+            cur.insertNamespace("p14", p14Ns);\r
+            cur.insertAttributeWithValue(new QName(STRelationshipId.type.getName().getNamespaceURI(), "embed"), prsEmbed1.getId());\r
+            cur.beginElement(new QName(p14Ns, "trim", "p14"));\r
+            cur.insertAttributeWithValue("st", df_time.format(seconds*1000.0));\r
+        } finally {\r
+            cur.dispose();\r
+        }\r
     }\r
 \r
     static void addTimingInfo(XSLFSlide slide1, XSLFPictureShape pic1) {\r
index 9fcf3120117401fad35b03f2fb55f98a2db25a7d..265fa730d48c8c9dec0c559b8c70d0a680961a19 100644 (file)
@@ -85,12 +85,14 @@ public final class LinkVideoToPptx {
 
             String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
             XmlCursor cur = ext.newCursor();
-            cur.toEndToken();
-            cur.beginElement(new QName(p14Ns, "media", "p14"));
-            cur.insertNamespace("p14", p14Ns);
-            cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId());
-            cur.dispose();
-
+            try {
+                cur.toEndToken();
+                cur.beginElement(new QName(p14Ns, "media", "p14"));
+                cur.insertNamespace("p14", p14Ns);
+                cur.insertAttributeWithValue(new QName(CORE_PROPERTIES_ECMA376_NS, "link"), prsEmbed1.getId());
+            } finally {
+                cur.dispose();
+            }
 
             CTSlide xslide = slide1.getXmlObject();
             CTTimeNodeList ctnl;