THRIFT-939. java: optional binary fields throw NPE on default byte[] getters
authorBryan Duxbury <bryanduxbury@apache.org>
Thu, 30 Sep 2010 19:36:05 +0000 (19:36 +0000)
committerBryan Duxbury <bryanduxbury@apache.org>
Thu, 30 Sep 2010 19:36:05 +0000 (19:36 +0000)
This patch deals with null ByteBuffers correctly.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1003212 13f79535-47bb-0310-9956-ffa450edef68

lib/java/src/org/apache/thrift/TBaseHelper.java
lib/java/test/org/apache/thrift/TestTBaseHelper.java

index 211ea62..2837d0b 100644 (file)
@@ -277,6 +277,9 @@ public final class TBaseHelper {
   }
 
   public static ByteBuffer copyBinary(final ByteBuffer orig) {
+    if (orig == null) {
+      return null;
+    }
     ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
     if (orig.hasArray()) {
       System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
@@ -288,6 +291,10 @@ public final class TBaseHelper {
   }
 
   public static byte[] copyBinary(final byte[] orig) {
+    if (orig == null) {
+      return null;
+    }
+
     byte[] copy = new byte[orig.length];
     System.arraycopy(orig, 0, copy, 0, orig.length);
     return copy;
index a66e789..6d72ad8 100644 (file)
@@ -173,6 +173,8 @@ public class TestTBaseHelper extends TestCase {
     assertEquals(1, b.position());
     b.reset();
     assertEquals(0, b.position());
+
+    assertNull(TBaseHelper.copyBinary((ByteBuffer)null));
   }
 
   public void testCopyBinaryWithByteArray() throws Exception {
@@ -180,5 +182,7 @@ public class TestTBaseHelper extends TestCase {
     byte[] copy = TBaseHelper.copyBinary(bytes);
     assertEquals(ByteBuffer.wrap(bytes), ByteBuffer.wrap(copy));
     assertNotSame(bytes, copy);
+
+    assertNull(TBaseHelper.copyBinary((byte[])null));
   }
 }