camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1124595 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/GenericFileConverter.java test/java/org/apache/camel/component/file/GenericFileConverterTest.java
Date Thu, 19 May 2011 08:36:40 GMT
Author: davsclaus
Date: Thu May 19 08:36:40 2011
New Revision: 1124595

URL: http://svn.apache.org/viewvc?rev=1124595&view=rev
Log:
CAMEL-3962: java.io.File -> InputStream converter should favor using FileInputStream instead
of loading content into memory.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java?rev=1124595&r1=1124594&r2=1124595&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConverter.java
Thu May 19 08:36:40 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.file;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Serializable;
@@ -63,7 +65,11 @@ public final class GenericFileConverter 
     @Converter
     public static InputStream genericFileToInputStream(GenericFile<?> file, Exchange
exchange) throws IOException {
         if (exchange != null) {
-            // ensure the body is loaded as we want the input stream of the body
+            // use a file input stream if its a java.io.File
+            if (file.getFile() instanceof java.io.File) {
+                return new FileInputStream((File) file.getFile());
+            }
+            // otherwise ensure the body is loaded as we want the input stream of the body
             file.getBinding().loadContent(exchange, file);
             return exchange.getContext().getTypeConverter().convertTo(InputStream.class,
exchange, file.getBody());
         } else {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java?rev=1124595&r1=1124594&r2=1124595&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/GenericFileConverterTest.java
Thu May 19 08:36:40 2011
@@ -17,11 +17,13 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.Serializable;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
@@ -145,4 +147,32 @@ public class GenericFileConverterTest ex
         assertMockEndpointsSatisfied();
     }
 
+    public void testToFileInputStream() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file://target/gf")
+                    .convertBodyTo(InputStream.class)
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws Exception {
+                            Object body = exchange.getIn().getBody();
+                            assertIsInstanceOf(FileInputStream.class, body);
+                        }
+                    })
+                    .to("mock:result");
+            }
+        });
+        context.start();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(FileInputStream.class);
+        mock.message(0).body(String.class).isEqualTo("Hello World");
+
+        template.sendBodyAndHeader("file://target/gf", "Hello World", Exchange.FILE_NAME,
"hello.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
 }



Mime
View raw message