From ca22a79e014e4c5c9786998ca9fb1a7dda73104f Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sat, 9 Jul 2016 06:30:12 +0000 Subject: [PATCH] close streams if an exception is throw git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751983 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/poifs/dev/POIFSDump.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/java/org/apache/poi/poifs/dev/POIFSDump.java b/src/java/org/apache/poi/poifs/dev/POIFSDump.java index 37d2dc31e..9d53d2185 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSDump.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSDump.java @@ -63,32 +63,37 @@ public class POIFSDump { System.out.println("Dumping " + filename); FileInputStream is = new FileInputStream(filename); - NPOIFSFileSystem fs = new NPOIFSFileSystem(is); - is.close(); - - DirectoryEntry root = fs.getRoot(); - File file = new File(new File(filename).getName(), root.getName()); - if (!file.exists() && !file.mkdirs()) { - throw new IOException("Could not create directory " + file); + NPOIFSFileSystem fs; + try { + fs = new NPOIFSFileSystem(is); + } finally { + is.close(); } - - dump(root, file); - - if (dumpProps) { - HeaderBlock header = fs.getHeaderBlock(); - dump(fs, header.getPropertyStart(), "properties", file); - } - if (dumpMini) { - NPropertyTable props = fs.getPropertyTable(); - int startBlock = props.getRoot().getStartBlock(); - if (startBlock == POIFSConstants.END_OF_CHAIN) { - System.err.println("No Mini Stream in file"); - } else { - dump(fs, startBlock, "mini-stream", file); + try { + DirectoryEntry root = fs.getRoot(); + File file = new File(new File(filename).getName(), root.getName()); + if (!file.exists() && !file.mkdirs()) { + throw new IOException("Could not create directory " + file); } + + dump(root, file); + + if (dumpProps) { + HeaderBlock header = fs.getHeaderBlock(); + dump(fs, header.getPropertyStart(), "properties", file); + } + if (dumpMini) { + NPropertyTable props = fs.getPropertyTable(); + int startBlock = props.getRoot().getStartBlock(); + if (startBlock == POIFSConstants.END_OF_CHAIN) { + System.err.println("No Mini Stream in file"); + } else { + dump(fs, startBlock, "mini-stream", file); + } + } + } finally { + fs.close(); } - - fs.close(); } }