camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r722009 - in /activemq/camel/branches/camel-1.x: ./ camel-core/src/main/java/org/apache/camel/converter/IOConverter.java camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
Date Mon, 01 Dec 2008 09:19:41 GMT
Author: ningjiang
Date: Mon Dec  1 01:19:40 2008
New Revision: 722009

URL: http://svn.apache.org/viewvc?rev=722009&view=rev
Log:
Merged revisions 722005 via svnmerge from 
https://svn.apache.org/repos/asf/activemq/camel/trunk

........
  r722005 | ningjiang | 2008-12-01 17:06:48 +0800 (Mon, 01 Dec 2008) | 1 line
  
  CAMEL-1134 make the ZipDataFormat Stream friendly
........

Modified:
    activemq/camel/branches/camel-1.x/   (props changed)
    activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
    activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java

Propchange: activemq/camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Dec  1 01:19:40 2008
@@ -1 +1 @@
-/activemq/camel/trunk:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713136,713273,713290,713292,713295,713314,713475,713625,713932,713944,714032,717965,717989,718242,718273,718312-718515,719163-719184,719334,719339,719524,719662,719848,719851,719855,719864,719978-719979,720207,720435-720437,720806,721272,721331,721333-721334,721360,721669,721764,721813,721985
+/activemq/camel/trunk:709850,711200,711206,711219-711220,711523,711531,711756,711784,711859,711874,711962,711971,712064,712119,712148,712662,712692,712925,713013,713107,713136,713273,713290,713292,713295,713314,713475,713625,713932,713944,714032,717965,717989,718242,718273,718312-718515,719163-719184,719334,719339,719524,719662,719848,719851,719855,719864,719978-719979,720207,720435-720437,720806,721272,721331,721333-721334,721360,721669,721764,721813,721985,722005

Propchange: activemq/camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java?rev=722009&r1=722008&r2=722009&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
(original)
+++ activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
Mon Dec  1 01:19:40 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/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
URL: http://svn.apache.org/viewvc/activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java?rev=722009&r1=722008&r2=722009&view=diff
==============================================================================
--- activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
(original)
+++ activemq/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/impl/ZipDataFormat.java
Mon Dec  1 01:19:40 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();
     }
 



Mime
View raw message