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
diff --git a/lib/java/src/org/apache/thrift/TUnion.java b/lib/java/src/org/apache/thrift/TUnion.java
index 2af891b..e33c7f2 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.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 @@
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 b6032cc..a9cb9c5 100644
--- a/lib/java/test/org/apache/thrift/TestTUnion.java
+++ b/lib/java/test/org/apache/thrift/TestTUnion.java
@@ -183,4 +183,13 @@
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());
+ }
}