You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2008/12/01 10:06:48 UTC

svn commit: r722005 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: converter/IOConverter.java impl/ZipDataFormat.java

Author: ningjiang
Date: Mon Dec  1 01:06:48 2008
New Revision: 722005

URL: http://svn.apache.org/viewvc?rev=722005&view=rev
Log:
CAMEL-1134 make the ZipDataFormat Stream friendly

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=722005&r1=722004&r2=722005&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java Mon Dec  1 01:06:48 2008
@@ -120,13 +120,23 @@
     }
 
     @Converter
-    public static InputStream toInputStream(String text) {
+    public static InputStream toInputStream(String text, Exchange exchange) {
+        if (exchange != null) {
+            String charsetName = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
+            if (charsetName != null) {
+                try {
+                    return toInputStream(text.getBytes(charsetName));
+                } catch (UnsupportedEncodingException e) {
+                    LOG.warn("Can't convert the String into the bytes with the charset " + charsetName, e);
+                }
+            }
+        }
         return toInputStream(text.getBytes());
     }
 
     @Converter
-    public static InputStream toInputStream(BufferedReader buffer) throws IOException {
-        return toInputStream(toString(buffer));
+    public static InputStream toInputStream(BufferedReader buffer, Exchange exchange) throws IOException {
+        return toInputStream(toString(buffer), exchange);
     }
 
     @Converter
@@ -270,14 +280,14 @@
         return bos.toByteArray();
     }
 
-    protected static void copy(InputStream stream, ByteArrayOutputStream bos) throws IOException {
+    public static void copy(InputStream stream, OutputStream os) throws IOException {
         byte[] data = new byte[4096];
-        int read = stream.read(data);
-        while (read != -1) {
-            bos.write(data, 0, read);
+        int read = stream.read(data);        
+        while (read != -1) {            
+            os.write(data, 0, read);
             read = stream.read(data);
         }
-        bos.flush();
+        os.flush();
     }
 
     protected static String toString(Source source, Properties props) throws TransformerException, IOException {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java?rev=722005&r1=722004&r2=722005&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java Mon Dec  1 01:06:48 2008
@@ -18,15 +18,12 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.util.zip.Deflater;
-import java.util.zip.Inflater;
-
-import javax.xml.bind.annotation.XmlAttribute;
+import java.util.zip.DeflaterOutputStream;
+import java.util.zip.InflaterInputStream;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.Message;
 import org.apache.camel.converter.IOConverter;
 import org.apache.camel.spi.DataFormat;
 
@@ -43,47 +40,43 @@
         this.compressionLevel = compressionLevel;
     }
 
-    public void marshal(Exchange exchange, Object graph, OutputStream stream)
+    public void marshal(Exchange exchange, Object body, OutputStream stream)
         throws Exception {
-        
-        // Retrieve the message body as byte array 
-        byte[] input = (byte[]) exchange.getIn().getBody(byte[].class);
-        
-        // Create a Message Deflater
-        Deflater deflater = new Deflater(compressionLevel);
-        deflater.setInput(input);
-        deflater.finish();        
+        InputStream is;
+        if (body instanceof InputStream) {
+            is = (InputStream)body;
+        }
+        is = exchange.getIn().getBody(InputStream.class);
+        if (is == null) {
+            throw new IllegalArgumentException("Can't get the inputstream for ZipDataFormat mashalling");
+        }
+       
+        DeflaterOutputStream zipOutput = new DeflaterOutputStream(stream, new Deflater(compressionLevel));
         
         // Compress the data
-        byte[] output = new byte[INITIALBYTEARRAYSIZE];
-        while (!deflater.finished()) {
-            int count = deflater.deflate(output);
-            stream.write(output, 0, count);
-        }
+        IOConverter.copy(is, zipOutput);
+        zipOutput.close();
 
     }
 
     public Object unmarshal(Exchange exchange, InputStream stream)
         throws Exception {
-
-        // Retrieve the message body as byte array 
-        byte[] input = (byte[]) exchange.getIn().getBody(byte[].class);
         
-        // Create a Message Inflater        
-        Inflater inflater = new Inflater();
-        inflater.setInput(input);
-
+        InputStream is = stream;
+        if (is == null) {           
+            exchange.getIn().getBody(InputStream.class);
+        }
+        if (is == null) {
+            throw new IllegalArgumentException("Can't get the inputStream for ZipDataFormat unmashalling");
+        }
+        
+        InflaterInputStream unzipInput = new InflaterInputStream(is);
+        
         // Create an expandable byte array to hold the inflated data
-        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
         
-        // Inflate the compressed data
-        byte[] buf = new byte[INITIALBYTEARRAYSIZE];
-        while (!inflater.finished()) {
-            int count = inflater.inflate(buf);
-            bos.write(buf, 0, count);
-        }    
+        IOConverter.copy(unzipInput, bos);        
 
-        // Return the inflated data
         return bos.toByteArray();
     }