]> source.dussan.org Git - poi.git/commitdiff
Fix one more place where stream were left open when an exception is thrown
authorDominik Stadler <centic@apache.org>
Sun, 17 Jul 2016 21:18:12 +0000 (21:18 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 17 Jul 2016 21:18:12 +0000 (21:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753123 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/dev/FormulaViewer.java

index 78a2ba04b280b72fafe93cc8885026f4a5211190..3426d4a09ccbbc3202087d733ac3d462442b43de 100644 (file)
@@ -57,24 +57,26 @@ public class FormulaViewer
      */
     public void run() throws IOException {
         NPOIFSFileSystem fs  = new NPOIFSFileSystem(new File(file), true);
-        InputStream is = BiffViewer.getPOIFSInputStream(fs);
-        List<Record> records = RecordFactory.createRecords(is);
-
-        for (int k = 0; k < records.size(); k++)
-        {
-            Record record = records.get(k);
-
-            if (record.getSid() == FormulaRecord.sid)
-            {
-               if (list) {
-                    listFormula((FormulaRecord) record);
-               }else {
-                    parseFormulaRecord(( FormulaRecord ) record);
-               }
+        try {
+            InputStream is = BiffViewer.getPOIFSInputStream(fs);
+            try {
+                List<Record> records = RecordFactory.createRecords(is);
+
+                for (Record record : records) {
+                    if (record.getSid() == FormulaRecord.sid) {
+                        if (list) {
+                            listFormula((FormulaRecord) record);
+                        } else {
+                            parseFormulaRecord((FormulaRecord) record);
+                        }
+                    }
+                }
+            } finally {
+                is.close();
             }
+        } finally {
+            fs.close();
         }
-        is.close();
-        fs.close();
     }
     
     private void listFormula(FormulaRecord record) {
@@ -90,7 +92,7 @@ public class FormulaViewer
                numArg = String.valueOf(-1);
             }
             
-            StringBuffer buf = new StringBuffer();
+            StringBuilder buf = new StringBuilder();
             
             if (token instanceof ExpPtg) return;
             buf.append(token.toFormulaString());
@@ -154,7 +156,7 @@ public class FormulaViewer
 
     private String formulaString(FormulaRecord record) {
 
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
                Ptg[] tokens = record.getParsedExpression();
                for (Ptg token : tokens) {
                        buf.append( token.toFormulaString());