From 8784d6d6d7b2a623e517cfa350aff3b2337d20cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alain=20B=C3=A9arez?= Date: Tue, 21 May 2019 00:13:47 +0000 Subject: [PATCH] fix potential input resource leaks (LGTM) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1859591 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/examples/EmbeddedObjects.java | 7 +++++-- .../org/apache/poi/poifs/dev/POIFSLister.java | 10 ++++++---- .../poi/hdgf/extractor/VisioTextExtractor.java | 18 ++++++++++-------- .../src/org/apache/poi/hpbf/dev/PLCDumper.java | 11 ++++++----- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java index 79d9a2d3b7..cbd06c7256 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java @@ -33,8 +33,11 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class EmbeddedObjects { @SuppressWarnings("unused") public static void main(String[] args) throws Exception { - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(args[0])); - try (HSSFWorkbook workbook = new HSSFWorkbook(fs)) { + try ( + FileInputStream fis = new FileInputStream(args[0]); + POIFSFileSystem fs = new POIFSFileSystem(fis); + HSSFWorkbook workbook = new HSSFWorkbook(fs) + ) { for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) { //the OLE2 Class Name of the object String oleName = obj.getOLE2ClassName(); diff --git a/src/java/org/apache/poi/poifs/dev/POIFSLister.java b/src/java/org/apache/poi/poifs/dev/POIFSLister.java index 345d3c6a2c..5ed20c75ab 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSLister.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSLister.java @@ -68,9 +68,11 @@ public class POIFSLister { } public static void viewFileOld(final String filename, boolean withSizes) throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename)); - displayDirectory(fs.getRoot(), "", withSizes); - fs.close(); + try (FileInputStream fis = new FileInputStream(filename)) { + POIFSFileSystem fs = new POIFSFileSystem(fis); + displayDirectory(fs.getRoot(), "", withSizes); + fs.close(); + } } public static void displayDirectory(DirectoryNode dir, String indent, boolean withSizes) { @@ -92,7 +94,7 @@ public class POIFSLister { name = name.substring(1) + " <" + altname + ">"; } if (withSizes) { - size = " [" + doc.getSize() + " / 0x" + + size = " [" + doc.getSize() + " / 0x" + Integer.toHexString(doc.getSize()) + "]"; } System.out.println(newIndent + name + size); diff --git a/src/scratchpad/src/org/apache/poi/hdgf/extractor/VisioTextExtractor.java b/src/scratchpad/src/org/apache/poi/hdgf/extractor/VisioTextExtractor.java index 256f1f9ffc..d21a7e5d6a 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/extractor/VisioTextExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/extractor/VisioTextExtractor.java @@ -60,7 +60,7 @@ public final class VisioTextExtractor extends POIOLE2TextExtractor { /** * Locates all the text entries in the file, and returns their * contents. - * + * * @return An array of each Text item in the document */ public String[] getAllText() { @@ -106,7 +106,7 @@ public final class VisioTextExtractor extends POIOLE2TextExtractor { * Returns the textual contents of the file. * Each textual object's text will be separated * by a newline - * + * * @return All text contained in this document, separated by \n */ @Override @@ -128,12 +128,14 @@ public final class VisioTextExtractor extends POIOLE2TextExtractor { System.exit(1); } - VisioTextExtractor extractor = - new VisioTextExtractor(new FileInputStream(args[0])); + try (FileInputStream fis = new FileInputStream(args[0])) { + VisioTextExtractor extractor = + new VisioTextExtractor(fis); - // Print not PrintLn as already has \n added to it - System.out.print(extractor.getText()); - - extractor.close(); + // Print not PrintLn as already has \n added to it + System.out.print(extractor.getText()); + + extractor.close(); + } } } diff --git a/src/scratchpad/src/org/apache/poi/hpbf/dev/PLCDumper.java b/src/scratchpad/src/org/apache/poi/hpbf/dev/PLCDumper.java index 0d9d9ae71d..4438111e4e 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/dev/PLCDumper.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/dev/PLCDumper.java @@ -53,12 +53,13 @@ public final class PLCDumper { System.err.println(" PLCDumper "); System.exit(1); } - PLCDumper dump = new PLCDumper( - new FileInputStream(args[0]) - ); - System.out.println("Dumping " + args[0]); - dump.dumpPLC(); + try (FileInputStream fis = new FileInputStream(args[0])) { + PLCDumper dump = new PLCDumper(fis); + + System.out.println("Dumping " + args[0]); + dump.dumpPLC(); + } } private void dumpPLC() {