close streams if an exception is throw

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751983 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-07-09 06:30:12 +00:00
parent 909c07f37f
commit ca22a79e01

View File

@ -63,32 +63,37 @@ public class POIFSDump {
System.out.println("Dumping " + filename); System.out.println("Dumping " + filename);
FileInputStream is = new FileInputStream(filename); FileInputStream is = new FileInputStream(filename);
NPOIFSFileSystem fs = new NPOIFSFileSystem(is); NPOIFSFileSystem fs;
is.close(); try {
fs = new NPOIFSFileSystem(is);
DirectoryEntry root = fs.getRoot(); } finally {
File file = new File(new File(filename).getName(), root.getName()); is.close();
if (!file.exists() && !file.mkdirs()) {
throw new IOException("Could not create directory " + file);
} }
try {
dump(root, file); DirectoryEntry root = fs.getRoot();
File file = new File(new File(filename).getName(), root.getName());
if (dumpProps) { if (!file.exists() && !file.mkdirs()) {
HeaderBlock header = fs.getHeaderBlock(); throw new IOException("Could not create directory " + file);
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);
} }
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();
} }
} }