--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
Thanks for the reply Adam -I did a simple sub into my code with your
suggestion, but it doesn't work. However, now it's made me think of
more questions.....
If protocol buffers doesn't use object I/O streams, which streams does
it use? The argument in Java simply asks for an OutputStream
argument, and of course that could mean File, ByteArray, Object, Data,
etc....
And perhaps more importantly, will the choice of Java stream types
impact how GPB objects may be reconstituted in C++? At the simplest
level I assume this can be done:
I can create a GPB class called "GPBCar".
I have an outputstream (of some type, not sure!) over a socket in Java
I want to write to.
After creating my GPBCar object, I use the GPB "writeTo(output)"
method on the Java side.
My C++ app takes some form of this byte stream and reconstitutes my
GPBCar instance by using the equivalent of the "parseFrom" method from
the GPBCar.
My understanding is that I have converted my GPBCar to a series of
bytes that represent that GPBCar instance. Now I have a C++ app at the
other end of the socket connection. But will the series of bytes be
in the correct order (endianness?). Will the outputstream type be
particular to Java, or will I encounter problems by using certain
specific Java outputstreams? Should I only be using the
CodedDataOutputStream provided by GPB? (I note that there is
implementations of this stream in both the C++ and Java GPB
implementations).
I should add I've tried to find answers to these questions on the
board and web, but with no luck. Again, if anyone has a simple
example of how they used GPB to communicate over a socket from a Java
to a C++ app, I'd love to see it (or just point me in the right
direction).
On Dec 2, 1:14 pm, Adam Vartanian <flo...@google.com> wrote:
>
> Protocol buffer code doesn't use object streams, so when you pass one
> to its writeTo/parseFrom methods, you're actually passing the object
> stream as a regular OutputStream or InputStream, ie, your code is
> already serializing to and from bytes. If you'd rather be explicit
> about it, you can just switch your stream's type from
> ObjectOutputStream to OutputStream (similarly with Input) and it
> should continue to work the same. All that should require is instead
> of doing something like
>
> ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
> message.writeTo(output);
>
> you do something like
>
> OutputStream output = socket.getOutputStream();
> message.writeTo(output);
>
> For interacting with C++, you should be able to use the standard C++
> API for reading in messages, since the data is already on the wire in
> byte format.
>
> - Adam
When the Java test app works, I'd say the IO stream is transparent. I
use the same simple object instance each time (have a hard coded
method that simply returns the same object so it's identical), and I
simply call the toString of the instance to see what it is (before I
call writeTo on the server side, and after I call parseFrom on the
client side). When it doesn't work, I don't even get to parsing the
input stream.....
Concerning the IO stream types -well, I was just experimenting with
various IO stream types, and seeing if there was a difference. I
think I tried the simpler InputStream and OutputStream but couldn't
get it to work. I know for sure I could get File, Data and Object IO
streams to work without trouble (well, assuming I used the same IO
types on both client and server).
I keep coming around to the fact that it looks like a specific data
stream type in Java does do something to the byte stream (perhaps
adding extra bytes for a header or some sort of meta information like
you mentioned? And in Java you might need the same input stream to
read it properly? I dunno -but again from my original post, I want to
make sure if I do get that series of bytes over to a C++ app, it's in
a form that can be handled.).
Anyway, I'm going to experiment further to see what I get. If I get a
simple example working, I'll post it as I'm sure I can't be the first
person with this dilemma.
On Dec 2, 6:31 pm, Henner Zeller <henner.zel...@googlemail.com> wrote:
> Hi,
>
> transparent as in: the same data is written out that you put in. If
> you see a difference then this might be because the ObjectOutputStream
> might add something to the data you provide, such as writing
> delimiters or something. So I guess the simple write(byte[]) will not
> actually write the content of the arrays but as well the length or
> something (this is a wild guess - haven't looked at the sources, but
> it would explain what you see).
>
> Why do you use the ObjectOutputStream anyway instead of the simpler
> OutputStream ?
>