[411] in java-interest
Re: writing "structures" to streams...
daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Tue Jun 20 14:45:38 1995
Date: Tue, 20 Jun 1995 11:25:22 -0700
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: sdossick@cs.columbia.edu
Cc: java-interest@java.Eng.Sun.COM
Hi Steve,
> We've been playing with using Java/HotJava applets to connect to an
> internal data server and return info to the user. Unfortunately, our
> server expects us to send a discrete packet of data across the net and then
> wait for a response.
>
> In C code, we simply have a structure, which we cast to a (char *) buffer and
> write() into the socket.
>
> We'd love to be able to do this in Java, but you can't cast a class to a
> bytestream. We end up having to write each piece of the (large) struct
> separately. This gets rather tiring for a struct with 15 elements!
>
> Any suggestions? We've thought about implementing a write() method or
> something, which knew how to write the struct to a stream, but it seems
> redundant to do this for each structure.
>
> I'm hoping I missed something realy simple in the java toolkit :)
> Suggestions?
There is no simple way to do this right now. You could write a
native method that writes the contents of a Java object to a
stream but that would be totally non-portable (the layout of
objects varies between versions and implementations).
The best way of solving it is to add a write(DataOutputStream out)
method to the instance in question and write the fields out explicitly.
You would also need a constructor that can reconstruct the object
from a DataInputStream. That would be portable.
Note that java has a modifier called "transient". This is supposed
to be used during persitification of instances. This has not been
implemented.
Have fun,
Arthur van Hoff (avh@eng.sun.com)
http://java.sun.com/people/avh/
Sun Microsystems Inc, M/S UPAL02-301,
100 Hamilton Avenue, Palo Alto CA 94301, USA
Tel: +1 415 473 7242, Fax: +1 415 473 7104
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com