oops, forgot to add new file

This commit is contained in:
David Renshaw 2014-05-24 14:39:47 -04:00
parent 22a54fceaf
commit 94f76d3964

View file

@ -0,0 +1,29 @@
package org.capnproto;
import java.io.IOException;
import java.nio.channels.GatheringByteChannel;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public final class Serialize {
public static void writeMessage(GatheringByteChannel outputChannel,
MessageBuilder message) throws IOException {
ByteBuffer[] segments = message.getSegmentsForOutput();
int tableSize = (segments.length + 2) & (~1);
ByteBuffer table = ByteBuffer.allocate(4 * tableSize);
table.order(ByteOrder.LITTLE_ENDIAN);
table.putInt(0, segments.length - 1);
for (int i = 0; i < segments.length; ++i) {
table.putInt(4 * (i + 1), segments[i].limit() / 8);
}
// Any padding is already zeroed.
outputChannel.write(table);
outputChannel.write(segments);
}
}