[463] in java-interest
Sending Objects Over Sockets
daemon@ATHENA.MIT.EDU (Gregory Zoller - Imonics Developme)
Fri Jun 23 10:15:03 1995
From: Gregory Zoller - Imonics Development <gzoller@imonics.com>
Date: Fri, 23 Jun 1995 09:48:02 -0400
To: java-interest@java.sun.com
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.
(If there's already a way to do this... somebody please
clue me in :-) )
For example... this would be nice:
class ABC {
Long lvalue;
String words;
Integer ivalue;
public void write() {
byte[] b;
b = lvalue.pack() + words.pack() + ivalue.pack();
// send b.length bytes over socket connection
}
public void read() {
byte[] b;
// read byte array into b from socket
int offset = 0;
lvalue = Long.unpack( b, offset );
words = String.unpack( b, offset );
ivalue = Integer.unpack( b, offset );
}
}
Notes:
* I know pack/unpack for Strings is redundant, but it would be a
clean, consistent interface with the other types. For
implementation this could call the existing String-byte
conversion methods.
* Strings would either need to be null-terminated or a string
length would need to be prepended to the String's bytes during
the pack. This is so that unpack will know how long the string
should be, and update offset accordingly.
* All the unpack methods would automatically update the value of
'offset' based on the length of the type (or String). This
leaves the user in a proper state for the next successive unpack().
* The pack/unpack implementation would allow for more general use
of packed data... such as a storage-efficient format to conduct
any io.
If anyone is really challenged, there could also be an object-level version
of pack and unpack that would automagically pack/unpack all data members:
ABC myObj;
byte[] b = myObj.pack();
This would strictly be a "bonus", in addition to type-level pack/unpack,
because you might not always want to pack *all* the data members in a class.
An idea of merit?
Greg
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com