Author: janstey Date: Thu Sep 27 17:15:29 2012 New Revision: 1391106 URL: http://svn.apache.org/viewvc?rev=1391106&view=rev Log: CAMEL-5659 - Make the buffer size in CachedOutputStream configurable
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java?rev=1391106&r1=1391105&r2=1391106&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/CachedOutputStream.java Thu Sep 27 17:15:29 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().getProperty(BUFFER_SIZE); String hold = exchange.getContext().getProperty(THRESHOLD); String dir = exchange.getContext().getProperty(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 static class WrappedInputStream extends InputStream { private CachedOutputStream cachedOutputStream; Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java?rev=1391106&r1=1391105&r2=1391106&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/CachedOutputStreamTest.java Thu Sep 27 17:15:29 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); + } }