You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/12/01 01:11:30 UTC

(commons-validator) branch master updated: Use try-with-resources

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b4b6cae Use try-with-resources
8b4b6cae is described below

commit 8b4b6cae45c1e29efec78ebff6b738376e42fca2
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Nov 30 20:11:26 2023 -0500

    Use try-with-resources
    
    Use String#isEmpty()
---
 .../apache/commons/validator/ValidatorAction.java  | 59 +++++++++++-----------
 1 file changed, 29 insertions(+), 30 deletions(-)

diff --git a/src/main/java/org/apache/commons/validator/ValidatorAction.java b/src/main/java/org/apache/commons/validator/ValidatorAction.java
index 2f8b5577..63f853a1 100644
--- a/src/main/java/org/apache/commons/validator/ValidatorAction.java
+++ b/src/main/java/org/apache/commons/validator/ValidatorAction.java
@@ -408,47 +408,46 @@ public class ValidatorAction implements Serializable {
     }
 
     /**
-     * Read a javascript function from a file.
-     * @param javascriptFileName The file containing the javascript.
+     * Reads a javascript function from a file.
+     *
+     * @param javaScriptFileName The file containing the javascript.
      * @return The javascript function or null if it could not be loaded.
      */
-    private String readJavascriptFile(final String javascriptFileName) {
+    private String readJavascriptFile(final String javaScriptFileName) {
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
         if (classLoader == null) {
-            classLoader = this.getClass().getClassLoader();
+            classLoader = getClass().getClassLoader();
         }
-
-        InputStream is = classLoader.getResourceAsStream(javascriptFileName);
-        if (is == null) {
-            is = this.getClass().getResourceAsStream(javascriptFileName);
-        }
-
-        if (is == null) {
-            getLog().debug("  Unable to read javascript name "+javascriptFileName);
-            return null;
-        }
-
-        final StringBuilder buffer = new StringBuilder();
-        final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); // TODO encoding
-        try {
-            String line = null;
-            while ((line = reader.readLine()) != null) {
-                buffer.append(line).append("\n");
+        try (InputStream is = openInputStream(javaScriptFileName, classLoader)) {
+            if (is == null) {
+                getLog().debug("  Unable to read javascript name " + javaScriptFileName);
+                return null;
             }
-
-        } catch (final IOException e) {
-            getLog().error("Error reading javascript file.", e);
-
-        } finally {
+            final StringBuilder buffer = new StringBuilder();
+            final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); // TODO encoding
             try {
-                reader.close();
+                String line = null;
+                while ((line = reader.readLine()) != null) {
+                    buffer.append(line).append("\n");
+                }
             } catch (final IOException e) {
-                getLog().error("Error closing stream to javascript file.", e);
+                getLog().error("Error reading javascript file.", e);
+
             }
+            final String function = buffer.toString();
+            return function.isEmpty() ? null : function;
+        } catch (IOException e) {
+            getLog().error("Error closing stream to javascript file.", e);
+            return null;
         }
+    }
 
-        final String function = buffer.toString();
-        return function.equals("") ? null : function;
+    private InputStream openInputStream(final String javaScriptFileName, ClassLoader classLoader) {
+        InputStream is = classLoader.getResourceAsStream(javaScriptFileName);
+        if (is == null) {
+            is = this.getClass().getResourceAsStream(javaScriptFileName);
+        }
+        return is;
     }
 
     /**