[467] in java-interest
Re: Sending Objects Over Sockets
daemon@ATHENA.MIT.EDU (David Hopwood)
Fri Jun 23 12:08:25 1995
From: David Hopwood <david.hopwood@lady-margaret-hall.oxford.ac.uk>
To: gzoller@imonics.com (Gregory Zoller - Imonics Development)
Date: Fri, 23 Jun 1995 16:40:51 +0000 (BST)
Cc: java-interest@java.sun.com
In-Reply-To: <9506231348.AA08178@antioch.imonics.com> from "Gregory Zoller - Imonics Development" at Jun 23, 95 09:48:02 am
> In order to efficiently bundle objects and transmit them over
> a socket (without converting everything to ascii), it would be
> nice to have "pack" and "unpack" methods that would convert
> data to/from a byte array.
See the class API documentation on DataInputStream and DataOutputStream.
I recently posted a suggestion for more general way of doing this, which would
convert a network of objects, keeping internal pointers intact (the title was
Re: Java Objects over the Network).
Failing that, here's a quick hack that creates a single object of a
dynamically determined class. The class name is assumed to have been
written to a stream, followed by its data.
I haven't had a chance to test this, because I still haven't got a system that
will run Java :-(. But it shouldn't be too far off.
In a library somewhere:
interface Recreatable {
Object fromStream (DataInputStream);
}
class Extractor {
public static Object from (DataInputStream is) {
String classname = is.readLine ();
Recreatable r = (Recreatable) (Class.forName (classname).
newInstance ());
return r.fromStream (is);
}
}
and then in the client code:
DataInputStream is = ...;
Root theObject = (Root) (Extractor.from (is));
where Root is the interface of the object.
Classes implementing Root would be written as something like:
class Foo implements Root {
Foo () {};
Foo fromStream (DataInputStream is) {
/* unpack variables */; return self;
}
// other methods and data here
}
David Hopwood
david.hopwood@lmh.ox.ac.uk
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com