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

markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new da3250c3c1 Reduce duplication
da3250c3c1 is described below

commit da3250c3c1883e2a81b88445fc66f406bb2e9aee
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Nov 17 19:33:24 2026 +0000

    Reduce duplication
---
 java/org/apache/jasper/compiler/SmapUtil.java      | 14 +------
 java/org/apache/tomcat/util/buf/IOTools.java       | 48 ++++++++++++++++++++++
 test/org/apache/el/TesterIsolatedClassLoader.java  | 17 ++------
 .../jasper/servlet/TestJspCServletContext.java     | 16 ++------
 .../tomcat/security/TestSecurity2026Http2.java     | 16 +-------
 .../tomcat/util/scan/TestReferenceCountedJar.java  | 11 +----
 6 files changed, 60 insertions(+), 62 deletions(-)

diff --git a/java/org/apache/jasper/compiler/SmapUtil.java 
b/java/org/apache/jasper/compiler/SmapUtil.java
index 8f216716d8..efef84bae1 100644
--- a/java/org/apache/jasper/compiler/SmapUtil.java
+++ b/java/org/apache/jasper/compiler/SmapUtil.java
@@ -42,6 +42,7 @@ import org.apache.jasper.JspCompilationContext;
 import org.apache.jasper.compiler.SmapStratum.LineInfo;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.buf.IOTools;
 
 /**
  * Contains static utilities for generating SMAP data based on the current 
version of Jasper.
@@ -982,7 +983,7 @@ public class SmapUtil {
         try {
             is = cl.getResourceAsStream(className.replace(".", "/") + ".smap");
             if (is != null) {
-                return new String(readFully(is), SMAP_ENCODING);
+                return new String(IOTools.readFully(is), SMAP_ENCODING);
             }
 
             is = cl.getResourceAsStream(className.replace(".", "/") + 
".class");
@@ -1010,15 +1011,4 @@ public class SmapUtil {
         }
         return null;
     }
-
-    private static byte[] readFully(InputStream is) throws IOException {
-        byte[] buf = new byte[1024];
-        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
-            int read;
-            while ((read = is.read(buf)) >= 0) {
-                out.write(buf, 0, read);
-            }
-            return out.toByteArray();
-        }
-    }
 }
diff --git a/java/org/apache/tomcat/util/buf/IOTools.java 
b/java/org/apache/tomcat/util/buf/IOTools.java
new file mode 100644
index 0000000000..f449ae4fd0
--- /dev/null
+++ b/java/org/apache/tomcat/util/buf/IOTools.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.util.buf;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class IOTools {
+
+    private IOTools() {
+        // Hide default constructor.
+    }
+
+    /**
+     * Reads the {@link InputStream} to EOF and returns the bytes read in a 
byte array.
+     *
+     * @param is The stream to read
+     *
+     * @return An array containing all the bytes read from the stream
+     *
+     * @throws IOException if there is a problem reading the stream
+     */
+    public static byte[] readFully(InputStream is) throws IOException {
+        byte[] buf = new byte[1024];
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        int read;
+        while ((read = is.read(buf)) >= 0) {
+            out.write(buf, 0, read);
+        }
+        return out.toByteArray();
+    }
+
+}
diff --git a/test/org/apache/el/TesterIsolatedClassLoader.java 
b/test/org/apache/el/TesterIsolatedClassLoader.java
index a32b40744e..fafaaf5076 100644
--- a/test/org/apache/el/TesterIsolatedClassLoader.java
+++ b/test/org/apache/el/TesterIsolatedClassLoader.java
@@ -16,11 +16,12 @@
  */
 package org.apache.el;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.ref.WeakReference;
 
+import org.apache.tomcat.util.buf.IOTools;
+
 /**
  * Defines a copy of a single class in a class loader of its own so that tests 
can check that the class, and therefore
  * the class loader, is not kept in memory by a cache.
@@ -45,7 +46,7 @@ public class TesterIsolatedClassLoader extends ClassLoader {
             Class<?> result = findLoadedClass(name);
             if (result == null) {
                 try (InputStream is = 
getParent().getResourceAsStream(name.replace('.', '/') + ".class")) {
-                    byte[] bytes = readFully(is);
+                    byte[] bytes = IOTools.readFully(is);
                     result = defineClass(name, bytes, 0, bytes.length);
                 } catch (IOException e) {
                     throw new ClassNotFoundException(name, e);
@@ -56,18 +57,6 @@ public class TesterIsolatedClassLoader extends ClassLoader {
     }
 
 
-    private static byte[] readFully(InputStream is) throws IOException {
-        byte[] buf = new byte[1024];
-        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
-            int read;
-            while ((read = is.read(buf)) >= 0) {
-                out.write(buf, 0, read);
-            }
-            return out.toByteArray();
-        }
-    }
-
-
     /**
      * Waits until the referenced object has been garbage collected.
      *
diff --git a/test/org/apache/jasper/servlet/TestJspCServletContext.java 
b/test/org/apache/jasper/servlet/TestJspCServletContext.java
index 17e60af95e..166670de6f 100644
--- a/test/org/apache/jasper/servlet/TestJspCServletContext.java
+++ b/test/org/apache/jasper/servlet/TestJspCServletContext.java
@@ -19,7 +19,6 @@ package org.apache.jasper.servlet;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileWriter;
-import java.io.IOException;
 import java.io.InputStream;
 import java.net.JarURLConnection;
 import java.net.URL;
@@ -40,6 +39,8 @@ import org.junit.Assert;
 import org.junit.Assume;
 import org.junit.Test;
 
+import org.apache.tomcat.util.buf.IOTools;
+
 public class TestJspCServletContext {
 
     @Test
@@ -177,7 +178,7 @@ public class TestJspCServletContext {
             Assert.assertNotNull(url);
             try (InputStream is = url.openStream()) {
                 Assert.assertEquals("<jsp/>",
-                        new String(readFully(is), StandardCharsets.UTF_8));
+                        new String(IOTools.readFully(is), 
StandardCharsets.UTF_8));
             }
             Assert.assertNotNull(context.getResource("/unicode-\u00fc.jsp"));
         } finally {
@@ -185,17 +186,6 @@ public class TestJspCServletContext {
         }
     }
 
-    private static byte[] readFully(InputStream is) throws IOException {
-        byte[] buf = new byte[1024];
-        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
-            int read;
-            while ((read = is.read(buf)) >= 0) {
-                out.write(buf, 0, read);
-            }
-            return out.toByteArray();
-        }
-    }
-
     @Test
     public void testGetResourceFromResourceJarWithUriIllegalCharacters() 
throws Exception {
         // Resources in a resource JAR whose entry name contains characters
diff --git a/test/org/apache/tomcat/security/TestSecurity2026Http2.java 
b/test/org/apache/tomcat/security/TestSecurity2026Http2.java
index 5ade511033..24175645ec 100644
--- a/test/org/apache/tomcat/security/TestSecurity2026Http2.java
+++ b/test/org/apache/tomcat/security/TestSecurity2026Http2.java
@@ -18,7 +18,6 @@ package org.apache.tomcat.security;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Socket;
 import java.net.SocketTimeoutException;
@@ -45,6 +44,7 @@ import org.apache.catalina.Wrapper;
 import org.apache.catalina.startup.Tomcat;
 import org.apache.coyote.http2.Http2TestBase;
 import org.apache.coyote.http2.TesterHttp2Parser;
+import org.apache.tomcat.util.buf.IOTools;
 
 public class TestSecurity2026Http2 extends Http2TestBase {
 
@@ -190,7 +190,7 @@ public class TestSecurity2026Http2 extends Http2TestBase {
                 Thread.sleep(VICTIM_HANDLER_MS); // e.g. authentication 
back-end
             } catch (InterruptedException ignore) {
             }
-            byte[] body = readFully(req.getInputStream());
+            byte[] body = IOTools.readFully(req.getInputStream());
             resp.setContentType("text/plain");
             resp.getOutputStream().write(("echo:" + 
body.length).getBytes(StandardCharsets.ISO_8859_1));
         }
@@ -309,16 +309,4 @@ public class TestSecurity2026Http2 extends Http2TestBase {
         }
         return bytes.toByteArray();
     }
-
-
-    private static byte[] readFully(InputStream is) throws IOException {
-        byte[] buf = new byte[1024];
-        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
-            int read;
-            while ((read = is.read(buf)) >= 0) {
-                out.write(buf, 0, read);
-            }
-            return out.toByteArray();
-        }
-    }
 }
diff --git a/test/org/apache/tomcat/util/scan/TestReferenceCountedJar.java 
b/test/org/apache/tomcat/util/scan/TestReferenceCountedJar.java
index 33775f1815..a938abbc78 100644
--- a/test/org/apache/tomcat/util/scan/TestReferenceCountedJar.java
+++ b/test/org/apache/tomcat/util/scan/TestReferenceCountedJar.java
@@ -16,7 +16,6 @@
  */
 package org.apache.tomcat.util.scan;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -36,6 +35,7 @@ import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
 import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
+import org.apache.tomcat.util.buf.IOTools;
 
 public class TestReferenceCountedJar {
 
@@ -66,14 +66,7 @@ public class TestReferenceCountedJar {
 
 
     private static String readFully(InputStream is) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[1024];
-        int read = is.read(buffer);
-        while (read > 0) {
-            baos.write(buffer, 0, read);
-            read = is.read(buffer);
-        }
-        return baos.toString("UTF-8");
+        return new String(IOTools.readFully(is), StandardCharsets.UTF_8);
     }
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to