summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Duxbury <bryanduxbury@apache.org>2010-11-30 00:33:48 +0000
committerBryan Duxbury <bryanduxbury@apache.org>2010-11-30 00:33:48 +0000
commit708971351fd0136dce390ad3d76bb9f3e76ef9f6 (patch)
tree9bceeaec3ba9baeea34cd5e063297627d511911d
parent29208ff190ef0ee25116dd7e199457a5bea8f8e9 (diff)
downloadthrift-708971351fd0136dce390ad3d76bb9f3e76ef9f6.tar.gz
THRIFT-1009. java: TUnion does not correctly deep copy a ByteBuffer
This patch adds a case to deepCopyObject for ByteBuffer, along with a test case that verifies the change in functionality. git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1040358 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--lib/java/src/org/apache/thrift/TUnion.java8
-rw-r--r--lib/java/test/org/apache/thrift/TestTUnion.java9
2 files changed, 12 insertions, 5 deletions
diff --git a/lib/java/src/org/apache/thrift/TUnion.java b/lib/java/src/org/apache/thrift/TUnion.java
index 2af891b29..e33c7f216 100644
--- a/lib/java/src/org/apache/thrift/TUnion.java
+++ b/lib/java/src/org/apache/thrift/TUnion.java
@@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.nio.ByteBuffer;
import org.apache.thrift.protocol.TField;
import org.apache.thrift.protocol.TProtocol;
@@ -54,11 +55,8 @@ public abstract class TUnion<T extends TUnion, F extends TFieldIdEnum> implement
private static Object deepCopyObject(Object o) {
if (o instanceof TBase) {
return ((TBase)o).deepCopy();
- } else if (o instanceof byte[]) {
- byte[] other_val = (byte[])o;
- byte[] this_val = new byte[other_val.length];
- System.arraycopy(other_val, 0, this_val, 0, other_val.length);
- return this_val;
+ } else if (o instanceof ByteBuffer) {
+ return TBaseHelper.copyBinary((ByteBuffer)o);
} else if (o instanceof List) {
return deepCopyList((List)o);
} else if (o instanceof Set) {
diff --git a/lib/java/test/org/apache/thrift/TestTUnion.java b/lib/java/test/org/apache/thrift/TestTUnion.java
index b6032cc34..a9cb9c565 100644
--- a/lib/java/test/org/apache/thrift/TestTUnion.java
+++ b/lib/java/test/org/apache/thrift/TestTUnion.java
@@ -183,4 +183,13 @@ public class TestTUnion extends TestCase {
assertNull(tums.getSetField());
assertNull(tums.getFieldValue());
}
+
+ public void testDeepCopy() throws Exception {
+ byte[] bytes = {1, 2, 3};
+ ByteBuffer value = ByteBuffer.wrap(bytes);
+ ComparableUnion cu = ComparableUnion.binary_field(value);
+ ComparableUnion copy = cu.deepCopy();
+ assertEquals(cu, copy);
+ assertNotSame(cu.bufferForBinary_field().array(), copy.bufferForBinary_field().array());
+ }
}