THRIFT-1529. java: TupleProtocol can unintentionally include an extra byte in bit vectors when number of optional fields is an integral of 8
This patch harmonizes the math between writeBitSet and readBitSet to eliminate the mismatch in number of bytes calculation, allowing structs to be serialized correctly.
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1295995 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/org/apache/thrift/protocol/TTupleProtocol.java b/lib/java/src/org/apache/thrift/protocol/TTupleProtocol.java
index 14d50a6..74f5226 100644
--- a/lib/java/src/org/apache/thrift/protocol/TTupleProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TTupleProtocol.java
@@ -86,7 +86,7 @@
* @return a byte array of at least length 1
*/
public static byte[] toByteArray(BitSet bits, int vectorWidth) {
- byte[] bytes = new byte[vectorWidth / 8 + 1];
+ byte[] bytes = new byte[(int) Math.ceil(vectorWidth/8.0)];
for (int i = 0; i < bits.length(); i++) {
if (bits.get(i)) {
bytes[bytes.length - i / 8 - 1] |= 1 << (i % 8);