Oliver:

thanks for the reply.  I'm not going to use Object IO streams, rather
I used it as an example of how the IO stream type seems to make a
difference at each end.  Further, I now realize that once I use an IO
stream type, I must use the same type of stream at each ends of a Java
app.... I was under the mistaken impression that a stream produced by
a Java IO type is the same -i.e. just a stream of bytes with no meta
stuff -and that the type of Stream encapsulated the functionality
required for that type of stream (e.g. object, file, etc).

For communicating with a C++ app, I'm going to use a
bytearrayoutputstream.  I assume that the bytearrayoutputstream just
produces a simple stream of bytes -with no meta stuff and is able to
be used with C++.  Once I get a simple example up and running I'll
post as unless I missed an example in the tutorial or missed that
someone has posted one already, I haven't seen a real example of
sending an object over a socket from Java to C++ and vice versa.  I'm
guessing it might be a nice example to put up.

Anyway, for anyone else wanting to use ByteArrayStream in Java for
writing a Google , here is some code I've used:

ServerSocket serverSocket = new ServerSocket(2004, 10);
Socket socket = serverSocket.accept();
OutputStream outputStream = socket.getOutputStream();
outputStream.flush();

MyGPBObjects.MyGPBObject myGPBObject =
MyGPBObjectsFactory.instance.createMyGPBObject();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream
();
myGPBObject.writeTo(byteArrayOutputStream);
byteArrayOutputStream.flush();
byteArrayOutputStream.writeTo(outputStream);

Obviously without all the try/catches here and where myGPBObject is
the Google Protocol Buffers object I created (in my example I create
one from a factory called MyGPBObjectsFactory.)


On Dec 7, 4:35 am, Oliver Jowett <[email protected]> wrote:
> ObjectOutputStream doesn't just write raw bytes directly to the
> underlying stream, it also adds some additional header and framing data.
>
> So in general you can't write data with an ObjectOutputStream and read
> it back with a plain DataInputStream and expect things to work.
>
> There's really no reason to use ObjectOutputStream unless you are using
> Java serialization.
>
> -O

--

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.


Reply via email to