THRIFT-2612 Java TCompactProtocol.readBinary zero copying
Client: Java
Patch: Jon Hoffman
diff --git a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
index 75300b8..0a653a1 100644
--- a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
@@ -698,6 +698,12 @@
     checkStringReadLength(length);
     if (length == 0) return ByteBuffer.wrap(new byte[0]);
 
+    if (trans_.getBytesRemainingInBuffer() >= length) {
+      ByteBuffer bb = ByteBuffer.wrap(trans_.getBuffer(), trans_.getBufferPosition(), length);
+      trans_.consumeBuffer(length);
+      return bb;
+    }
+
     byte[] buf = new byte[length];
     trans_.readAll(buf, 0, length);
     return ByteBuffer.wrap(buf);