]> source.dussan.org Git - poi.git/commitdiff
Remove some findbugs warnings about missing close of streams, use existing IOUtils...
authorDominik Stadler <centic@apache.org>
Sat, 14 Mar 2015 13:18:43 +0000 (13:18 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 14 Mar 2015 13:18:43 +0000 (13:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1666683 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/dev/POIFSDump.java
src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java
src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java
src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java
src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java

index ace19d8d1820b38239d57b6fcdba841f7160ae56..66597fd4db5049029e116a5122249230d7df4a36 100644 (file)
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.FileOutputStream;
+import java.io.OutputStream;
 import java.util.Iterator;
 
 /**
@@ -49,8 +50,8 @@ public class POIFSDump {
 
 
     public static void dump(DirectoryEntry root, File parent) throws IOException {
-        for(Iterator it = root.getEntries(); it.hasNext();){
-            Entry entry = (Entry)it.next();
+        for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
+            Entry entry = it.next();
             if(entry instanceof DocumentNode){
                 DocumentNode node = (DocumentNode)entry;
                 DocumentInputStream is = new DocumentInputStream(node);
@@ -58,9 +59,12 @@ public class POIFSDump {
                 is.read(bytes);
                 is.close();
 
-                FileOutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
-                out.write(bytes);
-                out.close();
+                OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()));
+                try {
+                       out.write(bytes);
+                } finally {
+                       out.close();
+                }
             } else if (entry instanceof DirectoryEntry){
                 DirectoryEntry dir = (DirectoryEntry)entry;
                 File file = new File(parent, entry.getName());
index 4f3d6ca6508dd8bc65083486b62f2e099e0a0dfa..9dbddddd7f467491a36337a7b50c0c065d0fe9f5 100644 (file)
@@ -1409,8 +1409,11 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
                } catch (FileNotFoundException e) {
                        throw new IOException(e.getLocalizedMessage());
                }
-               this.save(fos);
-               fos.close();
+               try {
+                       this.save(fos);
+               } finally {
+                       fos.close();
+               }
        }
 
        /**
index 51ced4c2ccfc6d4d94ab73840123b4f1c1995cab..c6d307259eb8dc08588917e1492c20d2c2555cd2 100644 (file)
@@ -33,6 +33,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;\r
 import java.io.FileInputStream;\r
 import java.io.IOException;\r
+import java.io.InputStream;\r
 import java.io.OutputStream;\r
 import java.security.GeneralSecurityException;\r
 import java.security.MessageDigest;\r
@@ -242,12 +243,15 @@ public class AgileEncryptor extends Encryptor {
         LittleEndian.putLong(buf, 0, oleStreamSize);\r
         integrityMD.update(buf, 0, LittleEndian.LONG_SIZE);\r
         \r
-        FileInputStream fis = new FileInputStream(tmpFile);\r
-        int readBytes;\r
-        while ((readBytes = fis.read(buf)) != -1) {\r
-            integrityMD.update(buf, 0, readBytes);\r
+        InputStream fis = new FileInputStream(tmpFile);\r
+        try {\r
+            int readBytes;\r
+            while ((readBytes = fis.read(buf)) != -1) {\r
+                integrityMD.update(buf, 0, readBytes);\r
+            }\r
+        } finally {\r
+               fis.close();\r
         }\r
-        fis.close();\r
         \r
         byte hmacValue[] = integrityMD.doFinal();\r
         \r
index 62accc42f3723f67a2823d655a2e58d19c62e33c..86abf67af96880ef22d0ffc20dcbb7beb3bde87e 100644 (file)
@@ -38,7 +38,11 @@ public class PresetGeometries extends LinkedHashMap<String, CustomGeometry> {
         try {
             InputStream is =
                     XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml");
-            read(is);
+            try {
+               read(is);
+            } finally {
+               is.close();
+            }
         } catch (Exception e){
             throw new RuntimeException(e);
         }
index 7e449dad36d113933a43801d41867b6384feb48b..41fc335af077922cd1c253d43efedf00d1088bb9 100644 (file)
@@ -19,13 +19,12 @@ package org.apache.poi.xssf.dev;
 
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
+import org.apache.poi.util.IOUtils;
 import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
 
@@ -40,7 +39,11 @@ public final class XSSFDump {
         for (int i = 0; i < args.length; i++) {
             System.out.println("Dumping " + args[i]);
             ZipFile zip = new ZipFile(args[i]);
-            dump(zip);
+            try {
+               dump(zip);
+            } finally {
+               zip.close();
+            }
         }
     }
 
@@ -49,6 +52,7 @@ public final class XSSFDump {
         int sep = zipname.lastIndexOf('.');
         File root = new File(zipname.substring(0, sep));
         root.mkdir();
+        System.out.println("Dupming to directory " + root);
 
         Enumeration<? extends ZipEntry> en = zip.entries();
         while(en.hasMoreElements()){
@@ -61,30 +65,24 @@ public final class XSSFDump {
             }
 
             File f = new File(root, entry.getName());
-            FileOutputStream out = new FileOutputStream(f);
-
-            if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){
-                try {
-                    XmlObject xml = XmlObject.Factory.parse(zip.getInputStream(entry));
-                    XmlOptions options = new XmlOptions();
-                    options.setSavePrettyPrint();
-                    xml.save(out, options);
-                } catch (Exception e){
-                    System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
-                    dump(zip.getInputStream(entry), out);
+            OutputStream out = new FileOutputStream(f);
+            try {
+                if(entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")){
+                    try {
+                        XmlObject xml = XmlObject.Factory.parse(zip.getInputStream(entry));
+                        XmlOptions options = new XmlOptions();
+                        options.setSavePrettyPrint();
+                        xml.save(out, options);
+                    } catch (Exception e){
+                        System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
+                        IOUtils.copy(zip.getInputStream(entry), out);
+                    }
+                } else {
+                    IOUtils.copy(zip.getInputStream(entry), out);
                 }
-            } else {
-                dump(zip.getInputStream(entry), out);
+            } finally {
+               out.close();
             }
-            out.close();
-
         }
     }
-
-    protected static void dump(InputStream is, OutputStream out) throws IOException{
-        int pos;
-        byte[] chunk = new byte[2048];
-        while((pos = is.read(chunk)) > 0) out.write(chunk, 0, pos);
-
-    }
 }
index 332a496c6e3fbefe056a19f8371278d81327935f..f591d5a4d7879e73d275febca07e2cb68da099ae 100644 (file)
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.poi.hmef.Attachment;
 import org.apache.poi.hmef.HMEFMessage;
@@ -70,13 +71,14 @@ public final class HMEFContentsExtractor {
     * Extracts the RTF message body to the supplied file
     */
    public void extractMessageBody(File dest) throws IOException {
-      FileOutputStream fout = new FileOutputStream(dest);
-      
-      MAPIRtfAttribute body = (MAPIRtfAttribute)
-         message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
-      fout.write(body.getData());
-      
-      fout.close();
+      OutputStream fout = new FileOutputStream(dest);
+      try {
+          MAPIRtfAttribute body = (MAPIRtfAttribute)
+             message.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
+          fout.write(body.getData());
+      } finally {
+         fout.close();
+      }
    }
    
    /**
@@ -101,9 +103,12 @@ public final class HMEFContentsExtractor {
          
          // Save it
          File file = new File(dir, filename);
-         FileOutputStream fout = new FileOutputStream(file);
-         fout.write( att.getContents() );
-         fout.close();
+         OutputStream fout = new FileOutputStream(file);
+         try {
+                fout.write( att.getContents() );
+         } finally {
+                fout.close();
+         }
       }
    }
 }