From: Yegor Kozlov Date: Tue, 20 Apr 2010 13:06:59 +0000 (+0000) Subject: Properly close internal InputStream in ExtractorFactory#createExtractor(File), see... X-Git-Tag: REL_3_7_BETA1~91 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a4dfc23a0b2df137d6b7de95c1aadf005dca9299;p=poi.git Properly close internal InputStream in ExtractorFactory#createExtractor(File), see Bugzilla 49147 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@935900 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 23c88fd9a4..f3805fb6b8 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49147 - Properly close internal InputStream in ExtractorFactory#createExtractor(File) 49138 - Fixed locale-sensitive formatters in PackagePropertiesPart 49153 - Ensure that CTVectorVariant is included in poi-ooxml-schemas.jar 49146 - Added accessors to CoreProperties.Keywords diff --git a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java index ee3da8c614..6a2379e19a 100644 --- a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java @@ -119,18 +119,22 @@ public class ExtractorFactory { public static POITextExtractor createExtractor(File f) throws IOException, InvalidFormatException, OpenXML4JException, XmlException { - InputStream inp = new PushbackInputStream( - new FileInputStream(f), 8); - - if(POIFSFileSystem.hasPOIFSHeader(inp)) { - return createExtractor(new POIFSFileSystem(inp)); - } - if(POIXMLDocument.hasOOXMLHeader(inp)) { - inp.close(); - return createExtractor(OPCPackage.open(f.toString())); - } - throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file"); - } + InputStream inp = null; + try { + inp = new PushbackInputStream( + new FileInputStream(f), 8); + + if(POIFSFileSystem.hasPOIFSHeader(inp)) { + return createExtractor(new POIFSFileSystem(inp)); + } + if(POIXMLDocument.hasOOXMLHeader(inp)) { + return createExtractor(OPCPackage.open(f.toString())); + } + throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file"); + } finally { + if(inp != null) inp.close(); + } + } public static POITextExtractor createExtractor(InputStream inp) throws IOException, InvalidFormatException, OpenXML4JException, XmlException { // Figure out the kind of stream