Example #1
0
        public static long computeSerializedSizeInWords(Capnproto.MessageBuilder message)
        {
            java.nio.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)
            {
                java.nio.ByteBuffer s = segments[i];
                bytes += s.limit();
            }
            return(bytes / Capnproto.Constants.BYTES_PER_WORD);
        }
Example #2
0
        /// <exception cref="System.IO.IOException"/>
        public static void Write(java.nio.channels.WritableByteChannel outputChannel, Capnproto.MessageBuilder message)
        {
            java.nio.ByteBuffer[] segments = message.GetSegmentsForOutput();
            int tableSize = (segments.Length + 2) & (~1);

            java.nio.ByteBuffer table = java.nio.ByteBuffer.allocate(4 * tableSize);
            table.order(java.nio.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.
            while (table.hasRemaining())
            {
                outputChannel.Write(table);
            }
            foreach (java.nio.ByteBuffer buffer in segments)
            {
                while (buffer.hasRemaining())
                {
                    outputChannel.Write(buffer);
                }
            }
        }
Example #3
0
 /// <exception cref="System.IO.IOException"/>
 public static void WriteToUnbuffered(java.nio.channels.WritableByteChannel output, Capnproto.MessageBuilder message)
 {
     Capnproto.BufferedOutputStreamWrapper buffered = new Capnproto.BufferedOutputStreamWrapper(output);
     Write(buffered, message);
     buffered.Flush();
 }
Example #4
0
 /// <exception cref="System.IO.IOException"/>
 public static void Write(Capnproto.BufferedOutputStream output, Capnproto.MessageBuilder message)
 {
     Capnproto.PackedOutputStream packedOutputStream = new Capnproto.PackedOutputStream(output);
     Capnproto.Serialize.Write(packedOutputStream, message);
 }