You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2012/09/22 14:39:59 UTC

svn commit: r1388790 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/converter/stream/ components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/

Author: dkulp
Date: Sat Sep 22 12:39:58 2012
New Revision: 1388790

URL: http://svn.apache.org/viewvc?rev=1388790&view=rev
Log:
Some optimizations for writing the stream caches out to another stream to avoid some buffer copies

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java?rev=1388790&r1=1388789&r2=1388790&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java Sat Sep 22 12:39:58 2012
@@ -22,6 +22,9 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.FileChannel;
+import java.nio.channels.WritableByteChannel;
 
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.StreamCache;
@@ -33,7 +36,7 @@ public class FileInputStreamCache extend
 
     public FileInputStreamCache(File file) throws FileNotFoundException {
         this.file = file;
-        this.stream = IOHelper.buffered(new FileInputStream(file));
+        this.stream = null;
     }
     
     @Override
@@ -45,18 +48,36 @@ public class FileInputStreamCache extend
 
     @Override
     public void reset() {
-        try {
-            // reset by closing and creating a new stream based on the file
-            close();
-            // reset by creating a new stream based on the file
-            stream = IOHelper.buffered(new FileInputStream(file));
-        } catch (FileNotFoundException e) {
-            throw new RuntimeCamelException("Cannot reset stream from file " + file, e);
-        }            
+        // reset by closing and creating a new stream based on the file
+        close();
+        // reset by creating a new stream based on the file
+        stream = null;
+        
+        if (!file.exists()) {
+            throw new RuntimeCamelException("Cannot reset stream from file " + file);
+        }
     }
 
     public void writeTo(OutputStream os) throws IOException {
-        IOHelper.copy(getInputStream(), os);
+        if (stream == null) {
+            FileInputStream s = new FileInputStream(file);
+            long len = file.length();
+            WritableByteChannel out;
+            if (os instanceof WritableByteChannel) {
+                out = (WritableByteChannel)os;
+            } else {
+                out = Channels.newChannel(os);
+            }
+            FileChannel fc = s.getChannel();
+            long pos = 0;
+            while (pos < len) {
+                long i = fc.transferTo(pos, len - pos, out);
+                pos += i;
+            }
+            s.close();
+        } else {
+            IOHelper.copy(getInputStream(), os);
+        }
     }
 
     @Override
@@ -69,7 +90,10 @@ public class FileInputStreamCache extend
         return getInputStream().read();
     }
 
-    protected InputStream getInputStream() {
+    protected InputStream getInputStream() throws IOException {
+        if (stream == null) {
+            stream = IOHelper.buffered(new FileInputStream(file));
+        }
         return stream;
     }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java?rev=1388790&r1=1388789&r2=1388790&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java Sat Sep 22 12:39:58 2012
@@ -21,7 +21,6 @@ import java.io.IOException;
 import java.io.OutputStream;
 
 import org.apache.camel.StreamCache;
-import org.apache.camel.util.IOHelper;
 
 public class InputStreamCache extends ByteArrayInputStream implements StreamCache {
 
@@ -30,7 +29,7 @@ public class InputStreamCache extends By
     }
 
     public void writeTo(OutputStream os) throws IOException {
-        IOHelper.copy(this, os);
+        os.write(buf, pos, count - pos);
     }
 
 }

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java?rev=1388790&r1=1388789&r2=1388790&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java (original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java Sat Sep 22 12:39:58 2012
@@ -20,6 +20,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.List;
 
+import org.apache.camel.StreamCache;
 import org.apache.camel.util.IOHelper;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.interceptor.Fault;
@@ -48,9 +49,12 @@ public class RawMessageContentRedirectIn
         if (null != params) {
             InputStream is = (InputStream)params.get(0);
             OutputStream os = message.getContent(OutputStream.class);
-
             try {
-                IOUtils.copy(is, os);
+                if (is instanceof StreamCache) {
+                    ((StreamCache)is).writeTo(os);
+                } else {
+                    IOUtils.copy(is, os);
+                }
             } catch (Exception e) {
                 throw new Fault(e);
             } finally {