Author: davsclaus
Date: Fri May 28 07:39:02 2010
New Revision: 949124

URL: http://svn.apache.org/viewvc?rev=949124&view=rev
Log:
CAMEL-2762: Fixing Mina ByteBuffer toString coverter leaving ByteBuffer back in 
a its existing state.

Modified:
    
camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConverter.java
    
camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConverterTest.java

Modified: 
camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConverter.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConverter.java?rev=949124&r1=949123&r2=949124&view=diff
==============================================================================
--- 
camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConverter.java
 (original)
+++ 
camel/trunk/components/camel-mina/src/main/java/org/apache/camel/component/mina/MinaConverter.java
 Fri May 28 07:39:02 2010
@@ -39,9 +39,14 @@ public final class MinaConverter {
 
     @Converter
     public static byte[] toByteArray(ByteBuffer buffer) {
-        byte[] answer = new byte[buffer.remaining()];
-        buffer.get(answer);
-        return answer;
+        buffer.mark();
+        try {
+            byte[] answer = new byte[buffer.remaining()];
+            buffer.get(answer);
+            return answer;
+        } finally {
+            buffer.reset();
+        }
     }
 
     @Converter

Modified: 
camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConverterTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConverterTest.java?rev=949124&r1=949123&r2=949124&view=diff
==============================================================================
--- 
camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConverterTest.java
 (original)
+++ 
camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConverterTest.java
 Fri May 28 07:39:02 2010
@@ -52,6 +52,20 @@ public class MinaConverterTest extends T
         assertEquals("Hello World \u4f60\u597d", out);
     }
 
+    public void testToStringTwoTimes() throws UnsupportedEncodingException {
+        String in = "Hello World \u4f60\u597d";
+        ByteBuffer bb = ByteBuffer.wrap(in.getBytes("UTF-8"));
+        Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+        exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
+
+        String out = MinaConverter.toString(bb, exchange);
+        assertEquals("Hello World \u4f60\u597d", out);
+
+        // should be possible to convert to string without affecting the 
ByteBuffer
+        out = MinaConverter.toString(bb, exchange);
+        assertEquals("Hello World \u4f60\u597d", out);
+    }
+
     public void testToInputStream() throws Exception {
         byte[] in = "Hello World".getBytes();
         ByteBuffer bb = ByteBuffer.wrap(in);


Reply via email to