Re: [capnproto] Serialize from Object to ByteBuffer and vice versa

324 views
Skip to first unread message

David Renshaw

unread,
Oct 15, 2015, 9:13:59 AM10/15/15
to zaga.b...@gmail.com, Cap'n Proto

On Thu, Oct 15, 2015 at 12:37 AM, <zaga.b...@gmail.com> wrote:
Dear all
I try to serizalize from ByteBuffer to Object:

        org.capnproto.MessageReader message = org.capnproto.Serialize.read(new org.capnproto.ArrayInputStream(myByteBuffer));
        MyObject.Reader retObj = message.getRoot(MyObject.factory);


However, i got syntax error at line 1.

That code looks good to me. Can you show me some more context? What is the precise error that you see?

 

        org.capnproto.MessageReader message = org.capnproto.Serialize.read(myByteBuffer);

But it doesn't work at all. 

This also looks like it should work. Maybe `myByteBuffer` has some type other than `java.nio.ByteBuffer`?

 

And when I serizlize from Object to ByteBuffer:

        org.capnproto.MessageBuilder message = new org.capnproto.MessageBuilder();
        MyObject.Builder object = message.initRoot(MyObject.factory);
       
        //init object
       
        return message.getSegmentsForOutput()[0];

Do you think it's fine?

This will only return the first segment of the message. You should instead call `Serialize.write()`.


- David

zaga.b...@gmail.com

unread,
Oct 15, 2015, 9:29:05 PM10/15/15
to Cap'n Proto, zaga.b...@gmail.com, da...@sandstorm.io
Dear David
When I use org.capnproto.MessageReader message = org.capnproto.Serialize.read(new org.capnproto.ArrayInputStream(myByteBuffer));
I got  java.lang.IllegalArgumentException
    at java.nio.Buffer.limit(Buffer.java:275)
    at org.capnproto.ArrayInputStream.read(ArrayInputStream.java:41)
    at org.capnproto.Serialize.fillBuffer(Serialize.java:42)
    at org.capnproto.Serialize.read(Serialize.java:56)
    at org.capnproto.Serialize.read(Serialize.java:51)
And When I use org.capnproto.MessageReader message = org.capnproto.Serialize.read(myByteBuffer);
I got java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Buffer.java:506)
    at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:361)
    at org.capnproto.Serialize.read(Serialize.java:120)
    at org.capnproto.Serialize.read(Serialize.java:111)
How it can happened

zaga.b...@gmail.com

unread,
Oct 15, 2015, 11:17:52 PM10/15/15
to Cap'n Proto, zaga.b...@gmail.com, da...@sandstorm.io


Vào 20:13:59 UTC+7 Thứ Năm, ngày 15 tháng 10 năm 2015, David Renshaw đã viết:

And about this, because message.getSegmentsForOutput().length always is  1 so I think I only need get message.getSegmentsForOutput()[0];  After that, I print myBuffer and test it by deserialize into myObject again, And I got IndexOutOfBoundException like I said to you in the previous message.
And if I use org.Capnp.Serialize.write(), this command only write to a class implement WritableChannel, while I want to write a ByteBuffer. So I can't use this command,
Any recommend?

David Renshaw

unread,
Oct 16, 2015, 8:52:19 PM10/16/15
to Team Zaga, Cap'n Proto
This might happen if the cursor position of the ByteBuffer points to the end of the data, as it would immediately after you've written into the ByteBuffer. In such cases, you need to call ByteBuffer.flip() to reset the position.


David Renshaw

unread,
Oct 16, 2015, 8:55:55 PM10/16/15
to Team Zaga, Cap'n Proto
You could use MessageBuilder.computeSerializedSizeInWords() to get the length of the message, then allocate an ArrayOutputStream that's large enough to hold it.


zaga.b...@gmail.com

unread,
Oct 18, 2015, 10:05:23 PM10/18/15
to Cap'n Proto, zaga.b...@gmail.com, da...@sandstorm.io

OK, thank David. My problem was resolved !

Vào 07:55:55 UTC+7 Thứ Bảy, ngày 17 tháng 10 năm 2015, David Renshaw đã viết:
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages