Author: bryanduxbury
Date: Thu Sep 30 19:36:05 2010
New Revision: 1003212

URL: http://svn.apache.org/viewvc?rev=1003212&view=rev
Log:
THRIFT-939. java: optional binary fields throw NPE on default byte[] getters

This patch deals with null ByteBuffers correctly.

Modified:
    incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBaseHelper.java
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/TestTBaseHelper.java

Modified: incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBaseHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBaseHelper.java?rev=1003212&r1=1003211&r2=1003212&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBaseHelper.java 
(original)
+++ incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBaseHelper.java Thu 
Sep 30 19:36:05 2010
@@ -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;

Modified: 
incubator/thrift/trunk/lib/java/test/org/apache/thrift/TestTBaseHelper.java
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/TestTBaseHelper.java?rev=1003212&r1=1003211&r2=1003212&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/test/org/apache/thrift/TestTBaseHelper.java 
(original)
+++ incubator/thrift/trunk/lib/java/test/org/apache/thrift/TestTBaseHelper.java 
Thu Sep 30 19:36:05 2010
@@ -173,6 +173,8 @@ public class TestTBaseHelper extends Tes
     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 Tes
     byte[] copy = TBaseHelper.copyBinary(bytes);
     assertEquals(ByteBuffer.wrap(bytes), ByteBuffer.wrap(copy));
     assertNotSame(bytes, copy);
+
+    assertNull(TBaseHelper.copyBinary((byte[])null));
   }
 }


Reply via email to