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

poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java
poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java

index fd48c05462ecc658991de27c738b4eee3596de56..7a5fb93bb39750cc2295bf76c82bdc735b166c8f 100644 (file)
 ==================================================================== */
 package org.apache.poi.ooxml.dev;
 
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
@@ -47,7 +43,7 @@ import org.xml.sax.InputSource;
  * Reads a zipped OOXML file and produces a copy with the included
  * pretty-printed XML files.
  *
- *  This is useful for comparing OOXML files produced by different tools as the often
+ *  This is useful for comparing OOXML files produced by different tools as they often
  *  use different formatting of the XML.
  */
 public class OOXMLPrettyPrint {
@@ -108,7 +104,9 @@ public class OOXMLPrettyPrint {
                     pretty(document, out, 2);
                 } else {
                     System.out.println("Not pretty-printing non-XML file " + name);
-                    IOUtils.copy(file.getInputStream(entry), out);
+                    try (InputStream in = file.getInputStream(entry)) {
+                        IOUtils.copy(in, out);
+                    }
                 }
             } catch (Exception e) {
                 throw new IOException("While handling entry " + name, e);
index 2bf840a34f9958031379e10b6dac1b0155f4ddfd..6b119d6b734b484634f70e4fab207f779f9dbb59 100644 (file)
@@ -73,8 +73,11 @@ public final class PackageHelper {
                 dest.addRelationship(part.getPartName(), rel.getTargetMode(), rel.getRelationshipType());
                 part_tgt = dest.createPart(part.getPartName(), part.getContentType());
 
-                try (OutputStream out = part_tgt.getOutputStream()) {
-                    IOUtils.copy(part.getInputStream(), out);
+                try (
+                        InputStream in = part.getInputStream();
+                        OutputStream out = part_tgt.getOutputStream()
+                ) {
+                    IOUtils.copy(in, out);
                 }
 
                 if (part.hasRelationships()) {
index 1d6bc25a34153f211ec3528f3e99afb889626459..1365a494dc0a30b509ead9b86d6d28b6b128421a 100644 (file)
@@ -118,9 +118,9 @@ public final class EncryptedTempFilePackagePart extends PackagePart {
     }
 
     @Override
-    public boolean load(InputStream ios) throws InvalidFormatException {
+    public boolean load(InputStream is) throws InvalidFormatException {
        try (OutputStream os = getOutputStreamImpl()) {
-            IOUtils.copy(ios, os);
+            IOUtils.copy(is, os);
        } catch(IOException e) {
             throw new InvalidFormatException(e.getMessage(), e);
        }
index fd86f6ee8ccf70796da11a9b34a06154ba0d440b..dd53be0ea986ee65057227d5f6acef6846fbeb84 100644 (file)
@@ -112,10 +112,10 @@ public final class MemoryPackagePart extends PackagePart {
     }
 
     @Override
-    public boolean load(InputStream ios) throws InvalidFormatException {
+    public boolean load(InputStream is) throws InvalidFormatException {
         try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
             // Grab the data
-            IOUtils.copy(ios, baos);
+            IOUtils.copy(is, baos);
             // Save it
             data = baos.toByteArray();
         } catch (IOException e) {
index e3ea42c5d355200497fccba2e7341aa9ffb07c61..d1a3e3fd807002819c7307654ef3b67ca5b5861a 100644 (file)
@@ -117,9 +117,9 @@ public final class TempFilePackagePart extends PackagePart {
     }
 
     @Override
-    public boolean load(InputStream ios) throws InvalidFormatException {
+    public boolean load(InputStream is) throws InvalidFormatException {
        try (OutputStream os = getOutputStreamImpl()) {
-            IOUtils.copy(ios, os);
+            IOUtils.copy(is, os);
        } catch(IOException e) {
             throw new InvalidFormatException(e.getMessage(), e);
        }