From 6a223ec11ea62de018b84da8c41dff583814cb29 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 17 May 2016 08:44:21 +0000 Subject: [PATCH] BiffViewer: Close resources always git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744212 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/dev/BiffViewer.java | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/java/org/apache/poi/hssf/dev/BiffViewer.java b/src/java/org/apache/poi/hssf/dev/BiffViewer.java index ce42d4c5e..6ba9de7a3 100644 --- a/src/java/org/apache/poi/hssf/dev/BiffViewer.java +++ b/src/java/org/apache/poi/hssf/dev/BiffViewer.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.record.*; +import org.apache.poi.hssf.record.RecordFormatException; import org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException; import org.apache.poi.hssf.record.chart.*; import org.apache.poi.hssf.record.pivottable.DataItemRecord; @@ -43,11 +44,7 @@ import org.apache.poi.hssf.record.pivottable.ViewFieldsRecord; import org.apache.poi.hssf.record.pivottable.ViewSourceRecord; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; -import org.apache.poi.util.HexDump; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; -import org.apache.poi.util.StringUtil; +import org.apache.poi.util.*; /** * Utility for reading in BIFF8 records and displaying data from them. @@ -66,10 +63,10 @@ public final class BiffViewer { * *@param is the InputStream from which the records will be obtained *@return an array of Records created from the InputStream - *@exception RecordFormatException on error processing the InputStream + *@exception org.apache.poi.util.RecordFormatException on error processing the InputStream */ public static Record[] createRecords(InputStream is, PrintWriter ps, BiffRecordListener recListener, boolean dumpInterpretedRecords) - throws RecordFormatException { + throws org.apache.poi.util.RecordFormatException { List temp = new ArrayList(); RecordInputStream recStream = new RecordInputStream(is); @@ -403,25 +400,34 @@ public final class BiffViewer { pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset())); } - NPOIFSFileSystem fs = new NPOIFSFileSystem(cmdArgs.getFile(), true); - InputStream is = getPOIFSInputStream(fs); - - if (cmdArgs.shouldOutputRawHexOnly()) { - int size = is.available(); - byte[] data = new byte[size]; + try { + NPOIFSFileSystem fs = new NPOIFSFileSystem(cmdArgs.getFile(), true); + try { + InputStream is = getPOIFSInputStream(fs); - is.read(data); - HexDump.dump(data, 0, System.out, 0); - } else { - boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations(); - boolean dumpHex = cmdArgs.shouldDumpBiffHex(); - boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign - runBiffViewer(pw, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump, - cmdArgs.suppressHeader()); - } - is.close(); - fs.close(); - pw.close(); + try { + if (cmdArgs.shouldOutputRawHexOnly()) { + int size = is.available(); + byte[] data = new byte[size]; + + is.read(data); + HexDump.dump(data, 0, System.out, 0); + } else { + boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations(); + boolean dumpHex = cmdArgs.shouldDumpBiffHex(); + boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign + runBiffViewer(pw, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump, + cmdArgs.suppressHeader()); + } + } finally { + is.close(); + } + } finally { + fs.close(); + } + } finally { + pw.close(); + } } protected static InputStream getPOIFSInputStream(NPOIFSFileSystem fs) @@ -472,7 +478,7 @@ public final class BiffViewer { return result; } private static String formatRecordDetails(int globalOffset, int sid, int size, int recordCounter) { - StringBuffer sb = new StringBuffer(64); + StringBuilder sb = new StringBuilder(64); sb.append("Offset=").append(HexDump.intToHex(globalOffset)).append("(").append(globalOffset).append(")"); sb.append(" recno=").append(recordCounter); sb.append( " sid=").append(HexDump.shortToHex(sid));