From 2f8c89683fc573ce09c1f8384d2642b69ca35756 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 14 Mar 2015 13:18:43 +0000 Subject: [PATCH] Remove some findbugs warnings about missing close of streams, use existing IOUtils.copy() to copy from one stream to another git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1666683 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/poifs/dev/POIFSDump.java | 14 ++++-- .../apache/poi/openxml4j/opc/OPCPackage.java | 7 ++- .../poi/poifs/crypt/agile/AgileEncryptor.java | 14 ++++-- .../poi/xslf/model/geom/PresetGeometries.java | 6 ++- .../org/apache/poi/xssf/dev/XSSFDump.java | 48 +++++++++---------- .../hmef/extractor/HMEFContentsExtractor.java | 25 ++++++---- 6 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/java/org/apache/poi/poifs/dev/POIFSDump.java b/src/java/org/apache/poi/poifs/dev/POIFSDump.java index ace19d8d18..66597fd4db 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSDump.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSDump.java @@ -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 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()); diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 4f3d6ca650..9dbddddd7f 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -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(); + } } /** diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java index 51ced4c2cc..c6d307259e 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java @@ -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 Encryptor { 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(); diff --git a/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java b/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java index 62accc42f3..86abf67af9 100644 --- a/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java +++ b/src/ooxml/java/org/apache/poi/xslf/model/geom/PresetGeometries.java @@ -38,7 +38,11 @@ public class PresetGeometries extends LinkedHashMap { try { InputStream is = XMLSlideShow.class.getResourceAsStream("presetShapeDefinitions.xml"); - read(is); + try { + read(is); + } finally { + is.close(); + } } catch (Exception e){ throw new RuntimeException(e); } diff --git a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java index 7e449dad36..41fc335af0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java +++ b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFDump.java @@ -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 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); - - } } diff --git a/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java b/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java index 332a496c6e..f591d5a4d7 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java @@ -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(); + } } } } -- 2.39.5