From 15c97c0941656d9c526efa16ffae514c039e77a0 Mon Sep 17 00:00:00 2001 From: Mathieu Stempell Date: Thu, 11 Jan 2018 15:33:45 +0100 Subject: [PATCH] closing InputStream created for URI to free the resource handle --- .../org/odftoolkit/odfdom/pkg/OdfPackage.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackage.java b/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackage.java index 82d8dc9f..b16fbc59 100644 --- a/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackage.java +++ b/odfdom/src/main/java/org/odftoolkit/odfdom/pkg/OdfPackage.java @@ -1585,16 +1585,22 @@ public Document getDom(String internalPath) throws SAXException, ParserConfigura * @throws java.lang.Exception In case the file could not be saved */ public void insert(URI sourceURI, String internalPath, String mediaType) throws Exception { - InputStream is = null; - if (sourceURI.isAbsolute()) { - // if the URI is absolute it can be converted to URL - is = sourceURI.toURL().openStream(); - } else { - // otherwise create a file class to open the stream - is = new FileInputStream(sourceURI.toString()); - } - insert(is, internalPath, mediaType); - } + InputStream is = null; + try { + if (sourceURI.isAbsolute()) { + // if the URI is absolute it can be converted to URL + is = sourceURI.toURL().openStream(); + } else { + // otherwise create a file class to open the stream + is = new FileInputStream(sourceURI.toString()); + } + insert(is, internalPath, mediaType); + } finally { + if (is != null) { + is.close(); + } + } + } /** * Inserts InputStream into an OdfPackage. An existing file will be replaced.