Wouldn't it be better to use the try / catch / close construct instead ? try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(outFile))) { ... }
2017-02-11 22:33 GMT+01:00 <fla...@apache.org>: > Repository: karaf > Updated Branches: > refs/heads/master 929a21c7c -> 17963db4e > > > Close unclosed JarOutputStream in WrapperSeviceImpl > > > Project: http://git-wip-us.apache.org/repos/asf/karaf/repo > Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/17963db4 > Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/17963db4 > Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/17963db4 > > Branch: refs/heads/master > Commit: 17963db4ee75f6ad18665438e0698a13700b9c68 > Parents: 929a21c > Author: Fabian Lange <lange.fab...@gmail.com> > Authored: Sat Feb 11 17:23:59 2017 +0100 > Committer: Fabian Lange <lange.fab...@gmail.com> > Committed: Sat Feb 11 15:33:39 2017 -0600 > > ---------------------------------------------------------------------- > .../apache/karaf/obr/command/util/FileUtil.java | 3 +-- > .../wrapper/internal/WrapperServiceImpl.java | 20 ++++++-------------- > 2 files changed, 7 insertions(+), 16 deletions(-) > ---------------------------------------------------------------------- > > > http://git-wip-us.apache.org/repos/asf/karaf/blob/17963db4/ > obr/src/main/java/org/apache/karaf/obr/command/util/FileUtil.java > ---------------------------------------------------------------------- > diff --git a/obr/src/main/java/org/apache/karaf/obr/command/util/FileUtil.java > b/obr/src/main/java/org/apache/karaf/obr/command/util/FileUtil.java > index 26aa0bd..31aed12 100644 > --- a/obr/src/main/java/org/apache/karaf/obr/command/util/FileUtil.java > +++ b/obr/src/main/java/org/apache/karaf/obr/command/util/FileUtil.java > @@ -80,8 +80,7 @@ public class FileUtil > > if (extract) > { > - is = new FileInputStream(file); > - JarInputStream jis = new JarInputStream(is); > + JarInputStream jis = new JarInputStream(new > FileInputStream(file)); > out.println("Extracting..."); > unjar(jis, dir); > jis.close(); > > http://git-wip-us.apache.org/repos/asf/karaf/blob/17963db4/ > wrapper/src/main/java/org/apache/karaf/wrapper/internal/ > WrapperServiceImpl.java > ---------------------------------------------------------------------- > diff --git > a/wrapper/src/main/java/org/apache/karaf/wrapper/internal/WrapperServiceImpl.java > b/wrapper/src/main/java/org/apache/karaf/wrapper/internal/ > WrapperServiceImpl.java > index 492a479..8bb7640 100644 > --- a/wrapper/src/main/java/org/apache/karaf/wrapper/internal/ > WrapperServiceImpl.java > +++ b/wrapper/src/main/java/org/apache/karaf/wrapper/internal/ > WrapperServiceImpl.java > @@ -399,21 +399,11 @@ public class WrapperServiceImpl implements > WrapperService { > } > } > > - private void safeClose(InputStream is) throws IOException { > - if (is == null) > + private void safeClose(Closeable c) throws IOException { > + if (c == null) > return; > try { > - is.close(); > - } catch (Throwable ignore) { > - // nothing to do > - } > - } > - > - private void safeClose(OutputStream is) throws IOException { > - if (is == null) > - return; > - try { > - is.close(); > + c.close(); > } catch (Throwable ignore) { > // nothing to do > } > @@ -447,8 +437,9 @@ public class WrapperServiceImpl implements > WrapperService { > if (is == null) { > throw new IllegalStateException("Resource " + resource + > " not found!"); > } > + JarOutputStream jar = null; > try { > - JarOutputStream jar = new JarOutputStream(new > FileOutputStream(outFile)); > + jar = new JarOutputStream(new FileOutputStream(outFile)); > int idx = resource.indexOf('/'); > while (idx > 0) { > jar.putNextEntry(new ZipEntry(resource.substring(0, > idx + 1))); > @@ -464,6 +455,7 @@ public class WrapperServiceImpl implements > WrapperService { > jar.close(); > } finally { > safeClose(is); > + safeClose(jar); > } > } > } > > -- ------------------------ Guillaume Nodet