You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/09/10 11:10:44 UTC

[2/4] git commit: CAMEL-6718: Marshal should leverage stream caching if enabled. Thanks to Franz Forsthofer for patch.

CAMEL-6718: Marshal should leverage stream caching if enabled. Thanks to Franz Forsthofer for patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/224836d0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/224836d0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/224836d0

Branch: refs/heads/master
Commit: 224836d008449337fc9dad102fa3417186c8aefd
Parents: 62b934d
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Sep 10 10:26:35 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Sep 10 10:26:35 2013 +0200

----------------------------------------------------------------------
 .../camel/processor/MarshalProcessor.java       | 27 ++++++++++++++++----
 1 file changed, 22 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/224836d0/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java
index 2c47081..c26d00a 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/MarshalProcessor.java
@@ -25,6 +25,7 @@ import org.apache.camel.CamelContextAware;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Traceable;
+import org.apache.camel.converter.stream.CachedOutputStream;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.support.ServiceSupport;
 import org.apache.camel.util.AsyncProcessorHelper;
@@ -35,7 +36,7 @@ import org.apache.camel.util.ServiceHelper;
  * Marshals the body of the incoming message using the given
  * <a href="http://camel.apache.org/data-format.html">data format</a>
  *
- * @version 
+ * @version
  */
 public class MarshalProcessor extends ServiceSupport implements AsyncProcessor, Traceable, CamelContextAware {
     private CamelContext camelContext;
@@ -52,7 +53,18 @@ public class MarshalProcessor extends ServiceSupport implements AsyncProcessor,
     public boolean process(Exchange exchange, AsyncCallback callback) {
         ObjectHelper.notNull(dataFormat, "dataFormat");
 
-        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        // if stream caching is enabled then use that so we can stream accordingly
+        // for example to overflow to disk for big streams
+        CachedOutputStream cos;
+        ByteArrayOutputStream os;
+        if (exchange.getContext().getStreamCachingStrategy().isEnabled()) {
+            cos = new CachedOutputStream(exchange);
+            os = null;
+        } else {
+            cos = null;
+            os = new ByteArrayOutputStream();
+        }
+
         Message in = exchange.getIn();
         Object body = in.getBody();
 
@@ -62,9 +74,14 @@ public class MarshalProcessor extends ServiceSupport implements AsyncProcessor,
         out.copyFrom(in);
 
         try {
-            dataFormat.marshal(exchange, body, buffer);
-            byte[] data = buffer.toByteArray();
-            out.setBody(data);
+            dataFormat.marshal(exchange, body, os);
+
+            if (cos != null) {
+                out.setBody(cos.newStreamCache());
+            } else {
+                byte[] data = os.toByteArray();
+                out.setBody(data);
+            }
         } catch (Exception e) {
             // remove OUT message, as an exception occurred
             exchange.setOut(null);