THRIFT-1101. java: bytebuffer length calculation in TBinaryProtocol writeBinary

This patch fixes a bug in Binary and Compact protocol that incorrectly calculates the length of the bytes to be written when the byte buffer being written has a nonzero array offset (such as after a slice() call).

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1083890 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bryan Duxbury 2011-03-21 18:18:33 +00:00
parent d3fceb02d4
commit 177b52ab58
3 changed files with 13 additions and 2 deletions

View File

@ -191,7 +191,7 @@ public class TBinaryProtocol extends TProtocol {
}
public void writeBinary(ByteBuffer bin) throws TException {
int length = bin.limit() - bin.position() - bin.arrayOffset();
int length = bin.limit() - bin.position();
writeI32(length);
trans_.write(bin.array(), bin.position() + bin.arrayOffset(), length);
}

View File

@ -306,7 +306,7 @@ public final 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() - bin.arrayOffset();
int length = bin.limit() - bin.position();
writeBinary(bin.array(), bin.position() + bin.arrayOffset(), length);
}

View File

@ -80,6 +80,17 @@ public abstract class ProtocolTestBase extends TestCase {
}
internalTestBinaryField(b);
}
if (canBeUsedNaked()) {
byte[] data = {1, 2, 3, 4, 5, 6};
TMemoryBuffer buf = new TMemoryBuffer(0);
TProtocol proto = getFactory().getProtocol(buf);
ByteBuffer bb = ByteBuffer.wrap(data);
bb.get();
proto.writeBinary(bb.slice());
assertEquals(ByteBuffer.wrap(data, 1, 5), proto.readBinary());
}
}
public void testString() throws Exception {