You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2015/03/14 14:18:44 UTC

svn commit: r1666683 - in /poi/trunk/src: java/org/apache/poi/poifs/dev/ ooxml/java/org/apache/poi/openxml4j/opc/ ooxml/java/org/apache/poi/poifs/crypt/agile/ ooxml/java/org/apache/poi/xslf/model/geom/ ooxml/java/org/apache/poi/xssf/dev/ scratchpad/src...

Author: centic
Date: Sat Mar 14 13:18:43 2015
New Revision: 1666683

URL: http://svn.apache.org/r1666683
Log:
Remove some findbugs warnings about missing close of streams, use existing IOUtils.copy() to copy from one stream to another

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

Modified: poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSDump.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSDump.java?rev=1666683&r1=1666682&r2=1666683&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSDump.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSDump.java Sat Mar 14 13:18:43 2015
@@ -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());

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java?rev=1666683&r1=1666682&r2=1666683&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java Sat Mar 14 13:18:43 2015
@@ -1409,8 +1409,11 @@ public abstract class OPCPackage impleme
 		} catch (FileNotFoundException e) {
 			throw new IOException(e.getLocalizedMessage());
 		}
-		this.save(fos);
-		fos.close();
+		try {
+			this.save(fos);
+		} finally {
+			fos.close();
+		}
 	}
 
 	/**

Modified: poi/trunk/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java?rev=1666683&r1=1666682&r2=1666683&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java Sat Mar 14 13:18:43 2015
@@ -33,6 +33,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.GeneralSecurityException;
 import java.security.MessageDigest;
@@ -242,12 +243,15 @@ public class AgileEncryptor extends Encr
         LittleEndian.putLong(buf, 0, oleStreamSize);
         integrityMD.update(buf, 0, LittleEndian.LONG_SIZE);
         
-        FileInputStream fis = new FileInputStream(tmpFile);
-        int readBytes;
-        while ((readBytes = fis.read(buf)) != -1) {
-            integrityMD.update(buf, 0, readBytes);
+        InputStream fis = new FileInputStream(tmpFile);
+        try {
+            int readBytes;
+            while ((readBytes = fis.read(buf)) != -1) {
+                integrityMD.update(buf, 0, readBytes);
+            }
+        } finally {
+        	fis.close();
         }
-        fis.close();
         
         byte hmacValue[] = integrityMD.doFinal();
         

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java?rev=1666683&r1=1666682&r2=1666683&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java Sat Mar 14 13:18:43 2015
@@ -38,7 +38,11 @@ public class PresetGeometries extends Li
         try {
             InputStream is =
                     XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml");
-            read(is);
+            try {
+            	read(is);
+            } finally {
+            	is.close();
+            }
         } catch (Exception e){
             throw new RuntimeException(e);
         }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java?rev=1666683&r1=1666682&r2=1666683&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java Sat Mar 14 13:18:43 2015
@@ -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);
-
-    }
 }

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java?rev=1666683&r1=1666682&r2=1666683&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java Sat Mar 14 13:18:43 2015
@@ -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();
+         }
       }
    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org