Implement computeSerializedSizeInWords for non-packed messages. Addresses #33.

This commit is contained in:
Mark Raynsford 2015-06-13 15:01:36 +00:00
parent eeb53b43b8
commit 63c56646de
No known key found for this signature in database
GPG key ID: AB9978DB38AB74C4

View file

@ -147,6 +147,31 @@ public final class Serialize {
return new MessageReader(segmentSlices, options); return new MessageReader(segmentSlices, options);
} }
public static long computeSerializedSizeInWords(MessageBuilder message)
{
final ByteBuffer[] segments = message.getSegmentsForOutput();
// From the capnproto documentation:
// "When transmitting over a stream, the following should be sent..."
long bytes = 0;
// "(4 bytes) The number of segments, minus one..."
bytes += 4;
// "(N * 4 bytes) The size of each segment, in words."
bytes += segments.length * 4;
// "(0 or 4 bytes) Padding up to the next word boundary."
if (bytes % 8 != 0) {
bytes += 4;
}
// The content of each segment, in order.
for (int i = 0; i < segments.length; ++i) {
final ByteBuffer s = segments[i];
bytes += s.limit();
}
return bytes / 8;
}
public static void write(WritableByteChannel outputChannel, public static void write(WritableByteChannel outputChannel,
MessageBuilder message) throws IOException { MessageBuilder message) throws IOException {
ByteBuffer[] segments = message.getSegmentsForOutput(); ByteBuffer[] segments = message.getSegmentsForOutput();