From 0e8cfd48dff4b8c76b1c1233214d616f34b4d3ea Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Mon, 2 Oct 2023 10:26:55 +0200 Subject: [PATCH] Close InputStream when done parsing configuration (https://issues.redhat.com/browse/JGRP-2731) --- src/org/jgroups/JChannel.java | 3 ++- src/org/jgroups/conf/ConfiguratorFactory.java | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/org/jgroups/JChannel.java b/src/org/jgroups/JChannel.java index b7d661d9ee0..5efacd55be0 100644 --- a/src/org/jgroups/JChannel.java +++ b/src/org/jgroups/JChannel.java @@ -107,7 +107,8 @@ public JChannel(String props) throws Exception { /** * Creates a channel with a configuration based on an input stream. - * @param input An input stream, pointing to a streamed configuration + * @param input An input stream, pointing to a streamed configuration. It is the caller's resposibility to close + * the input stream after the constructor returns */ public JChannel(InputStream input) throws Exception { this(ConfiguratorFactory.getStackConfigurator(input)); diff --git a/src/org/jgroups/conf/ConfiguratorFactory.java b/src/org/jgroups/conf/ConfiguratorFactory.java index ab92615af00..140abe49342 100644 --- a/src/org/jgroups/conf/ConfiguratorFactory.java +++ b/src/org/jgroups/conf/ConfiguratorFactory.java @@ -164,15 +164,15 @@ public static InputStream getConfigStream(Object properties) throws IOException */ static XmlConfigurator getXmlConfigurator(String properties) throws IOException { XmlConfigurator returnValue=null; - InputStream configStream=getConfigStream(properties); - if(configStream == null && properties.endsWith("xml")) - throw new FileNotFoundException(String.format(Util.getMessage("FileNotFound"), properties)); + try(InputStream configStream=getConfigStream(properties)) { + if(configStream == null && properties.endsWith("xml")) + throw new FileNotFoundException(String.format(Util.getMessage("FileNotFound"), properties)); - if (configStream != null) { - checkJAXPAvailability(); - returnValue=XmlConfigurator.getInstance(configStream); + if(configStream != null) { + checkJAXPAvailability(); + returnValue=XmlConfigurator.getInstance(configStream); + } } - return returnValue; }