THRIFT-939. java: optional binary fields throw NPE on default byte[] getters

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
diff --git a/lib/java/src/org/apache/thrift/TBaseHelper.java b/lib/java/src/org/apache/thrift/TBaseHelper.java
index 211ea62..2837d0b 100644
--- a/lib/java/src/org/apache/thrift/TBaseHelper.java
+++ b/lib/java/src/org/apache/thrift/TBaseHelper.java
@@ -277,6 +277,9 @@
   }
 
   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 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;
diff --git a/lib/java/test/org/apache/thrift/TestTBaseHelper.java b/lib/java/test/org/apache/thrift/TestTBaseHelper.java
index a66e789..6d72ad8 100644
--- a/lib/java/test/org/apache/thrift/TestTBaseHelper.java
+++ b/lib/java/test/org/apache/thrift/TestTBaseHelper.java
@@ -173,6 +173,8 @@
     assertEquals(1, b.position());
     b.reset();
     assertEquals(0, b.position());
+
+    assertNull(TBaseHelper.copyBinary((ByteBuffer)null));
   }
 
   public void testCopyBinaryWithByteArray() throws Exception {
@@ -180,5 +182,7 @@
     byte[] copy = TBaseHelper.copyBinary(bytes);
     assertEquals(ByteBuffer.wrap(bytes), ByteBuffer.wrap(copy));
     assertNotSame(bytes, copy);
+
+    assertNull(TBaseHelper.copyBinary((byte[])null));
   }
 }