Skip to content

Commit

Permalink
Fix to close input streams explicitly
Browse files Browse the repository at this point in the history
This fix introduces a try-with-resource block which automatically closes the resources by eliminating the need to call the close() method in the finally block explicitly.

resolve : #3041
  • Loading branch information
AmilaSamith committed Dec 11, 2023
1 parent 5b8aafe commit 61a97fc
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ private boolean lookupUtil(String key, URL url) {
if ("file".equals(url.getProtocol())) {
try {
if (new File(url.toURI()).exists()) {
try {
url.openStream();
try (InputStream inputStream = url.openStream()) {
// This try-with-resources block is used to check whether the resource is accessible
} catch (IOException e) {
log.error("Error occurred while accessing registry resource: " + key, e);
return true;
Expand Down Expand Up @@ -415,8 +415,8 @@ public RegistryEntry getRegistryEntry(String key) {
try {
URL url = new URL(resolveRegistryURI(key));
if ("file".equals(url.getProtocol())) {
try {
url.openStream();
try (InputStream inputStream = url.openStream()) {
// This try-with-resources block is used to check whether the resource is accessible
} catch (IOException e) {
log.error("Error occurred while accessing registry resource: " + key, e);
return null;
Expand Down Expand Up @@ -1133,8 +1133,8 @@ private void addConfigProperty(String name, String value) {
registryType = MicroIntegratorRegistryConstants.LOCAL_HOST_REGISTRY;

//Check existence of the target location
try {
rootPathUrl.openStream();
try (InputStream inputStream = rootPathUrl.openStream()) {
// This try-with-resources block is used to check whether the resource is accessible
} catch (IOException e) {
// If the registry is filesystem based, user may have provided the URI relative to the CARBON_HOME
if (log.isDebugEnabled()) {
Expand All @@ -1147,8 +1147,7 @@ private void addConfigProperty(String name, String value) {
}
pathFromCarbonHome = rootPathUrl.getProtocol() + ":" + pathFromCarbonHome + value;
rootPathUrl = new URL(pathFromCarbonHome);
try {
rootPathUrl.openStream();
try (InputStream inputStream = rootPathUrl.openStream()) {
value = pathFromCarbonHome;
} catch (IOException e1) {
//Unable to open input stream to target location
Expand Down

0 comments on commit 61a97fc

Please sign in to comment.