You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by li...@apache.org on 2023/02/10 09:18:43 UTC

[tomcat] branch 9.0.x updated: Replace manually invoke close method with try-with-resources. (#590)

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

lihan 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 90e6b33286 Replace manually invoke close method with try-with-resources. (#590)
90e6b33286 is described below

commit 90e6b332865109398e6fbab5de4657519ab8d9b2
Author: Andrei Briukhov <an...@gmail.com>
AuthorDate: Fri Feb 10 12:16:41 2023 +0300

    Replace manually invoke close method with try-with-resources. (#590)
---
 .../coyote/http11/filters/ChunkedOutputFilter.java | 30 ++++++++++++----------
 .../xreflection/ReflectionLessCodeGenerator.java   |  8 +++---
 .../apache/catalina/core/TestAsyncContextImpl.java | 13 +++-------
 3 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java b/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
index 6eedd81eea..c7a6f27a48 100644
--- a/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
+++ b/java/org/apache/coyote/http11/filters/ChunkedOutputFilter.java
@@ -177,20 +177,22 @@ public class ChunkedOutputFilter implements OutputFilter {
             lastChunk.position(0).limit(lastChunk.capacity());
 
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
-           OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.ISO_8859_1);
-            for (Map.Entry<String,String> trailerField : trailerFields.entrySet()) {
-                // Ignore disallowed headers
-                if (disallowedTrailerFieldNames.contains(
-                        trailerField.getKey().toLowerCase(Locale.ENGLISH))) {
-                    continue;
-                }
-                osw.write(trailerField.getKey());
-                osw.write(':');
-                osw.write(' ');
-                osw.write(trailerField.getValue());
-                osw.write("\r\n");
-            }
-            osw.close();
+
+           try (OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.ISO_8859_1)) {
+               for (Map.Entry<String, String> trailerField : trailerFields.entrySet()) {
+                   // Ignore disallowed headers
+                   if (disallowedTrailerFieldNames.contains(
+                           trailerField.getKey().toLowerCase(Locale.ENGLISH))) {
+                       continue;
+                   }
+                   osw.write(trailerField.getKey());
+                   osw.write(':');
+                   osw.write(' ');
+                   osw.write(trailerField.getValue());
+                   osw.write("\r\n");
+               }
+           }
+
             buffer.doWrite(ByteBuffer.wrap(baos.toByteArray()));
 
             buffer.doWrite(crlfChunk);
diff --git a/java/org/apache/tomcat/util/xreflection/ReflectionLessCodeGenerator.java b/java/org/apache/tomcat/util/xreflection/ReflectionLessCodeGenerator.java
index a06d78dce5..d79983e4f9 100644
--- a/java/org/apache/tomcat/util/xreflection/ReflectionLessCodeGenerator.java
+++ b/java/org/apache/tomcat/util/xreflection/ReflectionLessCodeGenerator.java
@@ -185,10 +185,10 @@ final class ReflectionLessCodeGenerator {
             .append(System.lineSeparator());
         //end - class
         File destination = new File(directory, className+".java");
-        BufferedWriter writer = new BufferedWriter(new FileWriter(destination, false));
-        writer.write(code.toString());
-        writer.flush();
-        writer.close();
+        try (BufferedWriter writer = new BufferedWriter(new FileWriter(destination, false))) {
+            writer.write(code.toString());
+            writer.flush();
+        }
 
     }
 
diff --git a/test/org/apache/catalina/core/TestAsyncContextImpl.java b/test/org/apache/catalina/core/TestAsyncContextImpl.java
index 1ce2eebb3e..1066c51b02 100644
--- a/test/org/apache/catalina/core/TestAsyncContextImpl.java
+++ b/test/org/apache/catalina/core/TestAsyncContextImpl.java
@@ -1521,15 +1521,10 @@ public class TestAsyncContextImpl extends TomcatBaseTest {
             // Just for debugging
             async.setTimeout(100000);
 
-            ExecutorService executor = Executors.newSingleThreadExecutor();
-            executor.submit(new Runnable() {
-
-                @Override
-                public void run() {
-                    async.dispatch("/ServletC");
-                }
-            });
-            executor.shutdown();
+            try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
+                executor.submit(() -> async.dispatch("/ServletC"));
+                executor.shutdown();
+            }
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org