Author: bryanduxbury
Date: Tue Nov 30 00:33:48 2010
New Revision: 1040358
URL: http://svn.apache.org/viewvc?rev=1040358&view=rev
Log:
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.
Modified:
thrift/trunk/lib/java/src/org/apache/thrift/TUnion.java
thrift/trunk/lib/java/test/org/apache/thrift/TestTUnion.java
Modified: thrift/trunk/lib/java/src/org/apache/thrift/TUnion.java
URL:
http://svn.apache.org/viewvc/thrift/trunk/lib/java/src/org/apache/thrift/TUnion.java?rev=1040358&r1=1040357&r2=1040358&view=diff
==============================================================================
--- thrift/trunk/lib/java/src/org/apache/thrift/TUnion.java (original)
+++ thrift/trunk/lib/java/src/org/apache/thrift/TUnion.java Tue Nov 30 00:33:48
2010
@@ -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 T
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) {
Modified: thrift/trunk/lib/java/test/org/apache/thrift/TestTUnion.java
URL:
http://svn.apache.org/viewvc/thrift/trunk/lib/java/test/org/apache/thrift/TestTUnion.java?rev=1040358&r1=1040357&r2=1040358&view=diff
==============================================================================
--- thrift/trunk/lib/java/test/org/apache/thrift/TestTUnion.java (original)
+++ thrift/trunk/lib/java/test/org/apache/thrift/TestTUnion.java Tue Nov 30
00:33:48 2010
@@ -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());
+ }
}