diff options
author | David Mollitor <dmollitor@apache.org> | 2020-10-02 15:10:50 -0400 |
---|---|---|
committer | David Mollitor <dmollitor@apache.org> | 2020-10-02 15:10:50 -0400 |
commit | a73edc55a3e540f8e9a37fe48486b92a1738eef5 (patch) | |
tree | f09d36f9475bffc43e5ab5e2fbb631a110239678 | |
parent | 1d68bfe3c4b72370e3db50f164c5241f15fc119c (diff) | |
download | thrift-a73edc55a3e540f8e9a37fe48486b92a1738eef5.tar.gz |
THRIFT-5288: Better Support for ByteBuffer in Compact Protocol
-rw-r--r-- | lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java | 4 | ||||
-rw-r--r-- | lib/java/src/org/apache/thrift/transport/TTransport.java | 17 |
2 files changed, 19 insertions, 2 deletions
diff --git a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java index 0dfcf25d1..6c08ef235 100644 --- a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java +++ b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java @@ -370,8 +370,8 @@ public class TCompactProtocol extends TProtocol { * Write a byte array, using a varint for the size. */ public void writeBinary(ByteBuffer bin) throws TException { - int length = bin.limit() - bin.position(); - writeBinary(bin.array(), bin.position() + bin.arrayOffset(), length); + writeVarint32(bin.remaining()); + writeBinary(bin); } private void writeBinary(byte[] buf, int offset, int length) throws TException { diff --git a/lib/java/src/org/apache/thrift/transport/TTransport.java b/lib/java/src/org/apache/thrift/transport/TTransport.java index 5645f7fa1..3811acd7e 100644 --- a/lib/java/src/org/apache/thrift/transport/TTransport.java +++ b/lib/java/src/org/apache/thrift/transport/TTransport.java @@ -22,6 +22,7 @@ package org.apache.thrift.transport; import org.apache.thrift.TConfiguration; import java.io.Closeable; +import java.nio.ByteBuffer; /** * Generic class that encapsulates the I/O layer. This is basically a thin @@ -121,6 +122,22 @@ public abstract class TTransport implements Closeable { throws TTransportException; /** + * Writes a sequence of bytes to the buffer. An attempt is made to write all + * remaining bytes in the buffer, that is, src.remaining(), at the moment this + * method is invoked. Upon return the buffer's position will updated; its limit + * will not have changed. Subclasses are encouraged to provide a more efficient + * implementation of this method. + * + * @param src The buffer from which bytes are to be retrieved + * @throws TTransportException if there was an error writing data + */ + public void write(ByteBuffer src) throws TTransportException { + byte[] arr = new byte[src.remaining()]; + src.get(arr); + write(arr); + } + + /** * Flush any pending data out of a transport buffer. * * @throws TTransportException if there was an error writing out data. |