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 2017/02/03 20:05:16 UTC

[3/3] camel git commit: CAMEL-10714 Replace ByteArrayOutputStream with OutputStreamBuilder

CAMEL-10714 Replace ByteArrayOutputStream with OutputStreamBuilder


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

Branch: refs/heads/camel-2.18.x
Commit: 799b78d9470c397217597fc719051a76c6c3d0a8
Parents: b265e3c
Author: Kevin Earls <ke...@kevinearls.com>
Authored: Fri Feb 3 16:08:35 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Feb 3 21:05:05 2017 +0100

----------------------------------------------------------------------
 .../component/jetty9/JettyContentExchange9.java | 27 ++++++++++++++++----
 1 file changed, 22 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/799b78d9/components/camel-jetty9/src/main/java/org/apache/camel/component/jetty9/JettyContentExchange9.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/main/java/org/apache/camel/component/jetty9/JettyContentExchange9.java b/components/camel-jetty9/src/main/java/org/apache/camel/component/jetty9/JettyContentExchange9.java
index 50f2e43..1f66312 100644
--- a/components/camel-jetty9/src/main/java/org/apache/camel/component/jetty9/JettyContentExchange9.java
+++ b/components/camel-jetty9/src/main/java/org/apache/camel/component/jetty9/JettyContentExchange9.java
@@ -37,8 +37,10 @@ import org.apache.camel.AsyncCallback;
 import org.apache.camel.CamelExchangeException;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.StreamCache;
 import org.apache.camel.component.jetty.JettyContentExchange;
 import org.apache.camel.component.jetty.JettyHttpBinding;
+import org.apache.camel.converter.stream.OutputStreamBuilder;
 import org.eclipse.jetty.client.HttpClient;
 import org.eclipse.jetty.client.api.Request;
 import org.eclipse.jetty.client.api.Response;
@@ -222,15 +224,18 @@ public class JettyContentExchange9 implements JettyContentExchange {
         };
 
         InputStreamResponseListener responseListener = new InputStreamResponseListener() {
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
 
             @Override
             public void onContent(Response response, ByteBuffer content, Callback callback) {
                 byte[] buffer = new byte[content.limit()];
                 content.get(buffer);
-                baos.write(buffer, 0, buffer.length);
-
-                callback.succeeded();
+                try {
+                    osb.write(buffer);
+                    callback.succeeded();
+                } catch (IOException e) {
+                    callback.failed(e);
+                }
             }
 
             @Override
@@ -238,7 +243,19 @@ public class JettyContentExchange9 implements JettyContentExchange {
                 if (result.isFailed()) {
                     doTaskCompleted(result.getFailure());
                 } else {
-                    onResponseComplete(result, baos.toByteArray());
+                    try {
+                        Object content = osb.build();
+                        if (content instanceof byte[]) {
+                            onResponseComplete(result, (byte[]) content);
+                        } else {
+                            StreamCache cos = (StreamCache) content;
+                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                            cos.writeTo(baos);
+                            onResponseComplete(result, baos.toByteArray());
+                        }
+                    } catch (IOException e) {
+                        doTaskCompleted(e);
+                    }
                 }
             }
         };