camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jans...@apache.org
Subject svn commit: r1391135 - in /camel/branches/camel-2.8.x/camel-core/src: main/java/org/apache/camel/converter/stream/CachedOutputStream.java test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
Date Thu, 27 Sep 2012 17:42:31 GMT
Author: janstey
Date: Thu Sep 27 17:42:31 2012
New Revision: 1391135

URL: http://svn.apache.org/viewvc?rev=1391135&view=rev
Log:
CAMEL-5659 - Make the buffer size in CachedOutputStream configurable

Conflicts:
	camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java

Modified:
    camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
    camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java

Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java?rev=1391135&r1=1391134&r2=1391135&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java
Thu Sep 27 17:42:31 2012
@@ -44,16 +44,18 @@ import org.slf4j.LoggerFactory;
  */
 public class CachedOutputStream extends OutputStream {
     public static final String THRESHOLD = "CamelCachedOutputStreamThreshold";
+    public static final String BUFFER_SIZE = "CamelCachedOutputStreamBufferSize";
     public static final String TEMP_DIR = "CamelCachedOutputStreamOutputDirectory";
     private static final transient Logger LOG = LoggerFactory.getLogger(CachedOutputStream.class);
     
-    private OutputStream currentStream = new ByteArrayOutputStream(2048);
+    private OutputStream currentStream;
     private boolean inMemory = true;
     private int totalLength;
     private File tempFile;
     private FileInputStreamCache fileInputStreamCache;
 
     private long threshold = 64 * 1024;
+    private int bufferSize = 2 * 1024;
     private File outputDir;
     
     public CachedOutputStream(Exchange exchange) {
@@ -61,14 +63,21 @@ public class CachedOutputStream extends 
     }
 
     public CachedOutputStream(Exchange exchange, boolean closedOnCompletion) {
+        String bufferSize = exchange.getContext().getProperties().get(BUFFER_SIZE);
         String hold = exchange.getContext().getProperties().get(THRESHOLD);
         String dir = exchange.getContext().getProperties().get(TEMP_DIR);
+        
+        if (bufferSize != null) {
+            this.bufferSize = exchange.getContext().getTypeConverter().convertTo(Integer.class,
bufferSize);
+        }
         if (hold != null) {
             this.threshold = exchange.getContext().getTypeConverter().convertTo(Long.class,
hold);
         }
         if (dir != null) {
             this.outputDir = exchange.getContext().getTypeConverter().convertTo(File.class,
dir);
         }
+       
+        currentStream = new ByteArrayOutputStream(this.bufferSize);
         
         if (closedOnCompletion) {
             // add on completion so we can cleanup after the exchange is done such as deleting
temporary files
@@ -164,7 +173,6 @@ public class CachedOutputStream extends 
         return new WrappedInputStream(this, getInputStream());
     }
 
-
     public StreamCache getStreamCache() throws IOException {
         flush();
 
@@ -215,6 +223,10 @@ public class CachedOutputStream extends 
         }
     }
     
+    public int getBufferSize() {
+        return bufferSize;
+    }
+
     // This class will close the CachedOutputStream when it is closed
     private class WrappedInputStream extends InputStream {
         private CachedOutputStream cachedOutputStream;

Modified: camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java?rev=1391135&r1=1391134&r2=1391135&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
(original)
+++ camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java
Thu Sep 27 17:42:31 2012
@@ -17,7 +17,10 @@
 package org.apache.camel.converter.stream;
 
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -152,4 +155,34 @@ public class CachedOutputStreamTest exte
 
         exchange.getUnitOfWork().done(exchange);
     }
+    
+    public void testCachedOutputStreamCustomBufferSize() throws IOException {
+        // double the default buffer size
+        context.getProperties().put(CachedOutputStream.BUFFER_SIZE, "4096");
+        
+        CachedOutputStream cos = new CachedOutputStream(exchange);
+        cos.write(TEST_STRING.getBytes("UTF-8"));
+
+        assertEquals("we should have a custom buffer size", cos.getBufferSize(), 4096);
+        
+        // make sure things still work after custom buffer size set
+        File file = new File("./target/cachedir");
+        String[] files = file.list();
+        assertEquals("we should have a temp file", files.length, 1);
+        assertTrue("The file name should start with cos" , files[0].startsWith("cos")); 
            
+        
+        StreamCache cache = cos.getStreamCache();
+        assertTrue("Should get the FileInputStreamCache", cache instanceof FileInputStreamCache);
+        String temp = toString((InputStream)cache);
+        assertEquals("Cached a wrong file", temp, TEST_STRING);
+        cache.reset();
+        temp = toString((InputStream)cache);
+        assertEquals("Cached a wrong file", temp, TEST_STRING);        
+        exchange.getUnitOfWork().done(exchange);
+        assertEquals("we should have a temp file", files.length, 1);
+        ((InputStream)cache).close();
+        
+        files = file.list();
+        assertEquals("we should have no temp file", files.length, 0);       
+    }
 }



Mime
View raw message