Me and a group of friends are working on a project in Java, and we need some help regarding sending objects through sockets.
So far, we have achieved to send simple objects (ints, strings and whatnot) through sockets, using ObjectOutputStream
and ObjectInputStream
. However, we ran into a huge problem today (huge for us, anyway ^^)
We have a tree structure, that we need to send from one PC to another. The problem is that, within each node of that tree, we have a reference to a BufferedImage and it's not serializable.
We have been researching a lot today, and we found out that we can use ImageIO.write()
to send one BufferedImage through the socket's OutputStream, however, it's no good to us since we don't need to send the BufferedImage by itself, but the whole tree were it is located.
What we need is a way (if it exists) to serialize each BufferedImage, converting it to another class if necessary, while making the tree, and having each node of the tree reference that new serializable class instead, so the tree can be sent as a whole object...
We really don't care about performance, since the trees we're sending aren't that big (10-15 nodes top). Thanks in advance for the help, sorry for the lousy English. Oh, and this is for a... well, a kind of homework, in case you want to keep that in mind :-)
Thanks!!
on each node you can use writeObject() and readObject() check http://java.sun.com/developer/technicalArticles/Programming/serialization/ for more info
essentially it will become
public Node implements Serializable{
transient BufferedImage buff;//transient make it so it won't be written with defaultWriteObject (which would error)
private void writeObject(ObjectOutputStream out)throws IOException{
out.defaultWriteObject();
//write buff with imageIO to out
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
in.defaultReadObject();
//read buff with imageIO from in
}
}