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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 8337effdb43 CAMEL-21293: camel-core - Unmarshal processor should close 
InputStream after processing
8337effdb43 is described below

commit 8337effdb43b4447265d28303c82f1d929b72e82
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Tue Oct 1 11:45:59 2024 +0200

    CAMEL-21293: camel-core - Unmarshal processor should close InputStream 
after processing
---
 .../src/main/java/org/apache/camel/spi/DataFormat.java       | 12 +++++++++++-
 .../apache/camel/support/processor/UnmarshalProcessor.java   |  7 ++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/DataFormat.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/DataFormat.java
index c461b8ad97e..4b0eedbb2d0 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/DataFormat.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/DataFormat.java
@@ -18,10 +18,12 @@ package org.apache.camel.spi;
 
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Iterator;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Service;
+import org.apache.camel.util.IOHelper;
 
 /**
  * Represents a <a href="http://camel.apache.org/data-format.html";>data 
format</a> used to marshal objects to and from
@@ -79,7 +81,15 @@ public interface DataFormat extends Service {
      * @throws Exception can be thrown
      */
     default Object unmarshal(Exchange exchange, Object body) throws Exception {
+        Object result = null;
         InputStream is = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, 
exchange, body);
-        return unmarshal(exchange, is);
+        try {
+            result = unmarshal(exchange, is);
+        } finally {
+            if (!(result instanceof Iterator)) {
+                IOHelper.close(is, "input stream");
+            }
+        }
+        return result;
     }
 }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/UnmarshalProcessor.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/UnmarshalProcessor.java
index 8b93cabd473..08054f8708e 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/UnmarshalProcessor.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/UnmarshalProcessor.java
@@ -78,7 +78,12 @@ public class UnmarshalProcessor extends 
AsyncProcessorSupport implements Traceab
                 // lets set up the out message before we invoke the dataFormat 
so that it can mutate it if necessary
                 out = exchange.getOut();
                 out.copyFrom(in);
-                result = dataFormat.unmarshal(exchange, body);
+                if (body instanceof InputStream is) {
+                    stream = is;
+                    result = dataFormat.unmarshal(exchange, stream);
+                } else {
+                    result = dataFormat.unmarshal(exchange, body);
+                }
             }
             if (result instanceof Exchange) {
                 if (result != exchange) {

Reply via email to