Author: dkulp
Date: Sat Sep 22 12:40:55 2012
New Revision: 1388791

URL: http://svn.apache.org/viewvc?rev=1388791&view=rev
Log:
Merged revisions 1388790 via  git cherry-pick from
https://svn.apache.org/repos/asf/camel/trunk

........
  r1388790 | dkulp | 2012-09-22 08:39:58 -0400 (Sat, 22 Sep 2012) | 2 lines

  Some optimizations for writing the stream caches out to another stream to 
avoid some buffer copies

........

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

Modified: 
camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java?rev=1388791&r1=1388790&r2=1388791&view=diff
==============================================================================
--- 
camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
 (original)
+++ 
camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/FileInputStreamCache.java
 Sat Sep 22 12:40:55 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/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java?rev=1388791&r1=1388790&r2=1388791&view=diff
==============================================================================
--- 
camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java
 (original)
+++ 
camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/converter/stream/InputStreamCache.java
 Sat Sep 22 12:40:55 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/branches/camel-2.10.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java?rev=1388791&r1=1388790&r2=1388791&view=diff
==============================================================================
--- 
camel/branches/camel-2.10.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java
 (original)
+++ 
camel/branches/camel-2.10.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageContentRedirectInterceptor.java
 Sat Sep 22 12:40:55 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 {


Reply via email to