camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [3/5] git commit: CAMEL-7209 Fixed the issue that NIOConverter.toByteArray return bad data with thanks to Radek
Date Wed, 26 Feb 2014 02:33:52 GMT
CAMEL-7209 Fixed the issue that NIOConverter.toByteArray return bad data with thanks to Radek


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/285dc7aa
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/285dc7aa
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/285dc7aa

Branch: refs/heads/camel-2.11.x
Commit: 285dc7aa1c5427922aace33aa96fa5d298563347
Parents: 06292ae
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Mon Feb 17 13:24:25 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Wed Feb 26 10:32:06 2014 +0800

----------------------------------------------------------------------
 .../java/org/apache/camel/converter/NIOConverter.java |  4 +++-
 .../org/apache/camel/converter/NIOConverterTest.java  | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/285dc7aa/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java b/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
index 42df28d..d0f86e7 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/NIOConverter.java
@@ -47,7 +47,9 @@ public final class NIOConverter {
 
     @Converter
     public static byte[] toByteArray(ByteBuffer buffer) {
-        return buffer.array();
+        byte[] bArray = new byte[buffer.limit()];
+        buffer.get(bArray);
+        return bArray;
     }
 
     @Converter

http://git-wip-us.apache.org/repos/asf/camel/blob/285dc7aa/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
index 3d85e5e..f1be12b 100644
--- a/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
+++ b/camel-core/src/test/java/org/apache/camel/converter/NIOConverterTest.java
@@ -34,6 +34,20 @@ public class NIOConverterTest extends ContextTestSupport {
         assertNotNull(out);
         assertEquals(5, out.length);
     }
+    
+    /**
+     * Test if returned array size is only to limit of ByteBuffer.
+     * 
+     * If byteBuffer capacity is bigger that limit, we MUST return data only to the limit.
+     */
+    public void testToByteArrayBigBuffer() {
+        ByteBuffer bb = ByteBuffer.allocate(100);
+        bb.put("Hello".getBytes());
+        bb.flip();
+        byte[] out = NIOConverter.toByteArray(bb);
+        assertNotNull(out);
+        assertEquals(5, out.length);
+    }
 
     public void testToString() throws Exception {
         ByteBuffer bb = ByteBuffer.wrap("Hello".getBytes());


Mime
View raw message