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

tilman pushed a commit to branch branch_2x
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/branch_2x by this push:
     new 5e3349515 Refactor parseToString method to use try-with-resources 
(#1946)
5e3349515 is described below

commit 5e3349515969d23319cb899f5505da72441da792
Author: DHL <[email protected]>
AuthorDate: Thu Sep 12 12:44:16 2024 +0900

    Refactor parseToString method to use try-with-resources (#1946)
    
    try-with-resources is used to automatically handle closing the InputStream, 
ensuring that it is always closed properly without the need for a finally block
---
 tika-core/src/main/java/org/apache/tika/Tika.java | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/Tika.java 
b/tika-core/src/main/java/org/apache/tika/Tika.java
index 22811f9c0..2f17de645 100644
--- a/tika-core/src/main/java/org/apache/tika/Tika.java
+++ b/tika-core/src/main/java/org/apache/tika/Tika.java
@@ -519,17 +519,15 @@ public class Tika {
     public String parseToString(InputStream stream, Metadata metadata, int 
maxLength)
             throws IOException, TikaException {
         WriteOutContentHandler handler = new WriteOutContentHandler(maxLength);
-        try {
-            ParseContext context = new ParseContext();
-            context.set(Parser.class, parser);
-            parser.parse(stream, new BodyContentHandler(handler), metadata, 
context);
+        ParseContext context = new ParseContext();
+        context.set(Parser.class, parser);
+        try (InputStream autoCloseStream = stream) {
+            parser.parse(autoCloseStream, new BodyContentHandler(handler), 
metadata, context);
         } catch (SAXException e) {
             if (!WriteLimitReachedException.isWriteLimitReached(e)) {
                 // This should never happen with BodyContentHandler...
                 throw new TikaException("Unexpected SAX processing failure", 
e);
             }
-        } finally {
-            stream.close();
         }
         return handler.toString();
     }

Reply via email to