Recent top five:
Java.next -- Four languages that represent the future of Java
Blogger Stuart Halloway has begun a series of posts on trends that point to the future of the Java platform. In his first
post, he compares Clojure, Groovy, JRuby, and Scala -- four wildly different languages that nonetheless all play together
in the JRE. Find out what unites these languages and what they can tell us about the future of Java-based development ...
| Enterprise AJAX - Transcend the Hype |
| Memory Analysis in Eclipse |
| Oracle Compatibility Developer's Guide |
| Memory Analysis in Eclipse |
ObjectInputStream and ObjectOutputStream. With this new API (the method writeObject(Object o) in class ObjectOutputStream and the method object readObject() in class ObjectInputStream), one can get a snapshot of running objects at any time, no matter how complex the object graph. Because such a snapshot
is provided through the ObjectOutputStream class, which is a descendent of the OutputStream class, one can easily wrap this stream with any other output stream, thereby implementing any desirable functionality (such
as a FileOutputStream).The presence of these new classes in Java 1.1 makes it possible to transmit a running object over the network. To do this,
the object, along with those objects that have been referenced, must be serializable -- that is, able to be converted into
a byte stream. Fortunately, in Java 1.1, most built-in classes are serializable. Some classes, however, are not (class Object is a prime example). Not to worry. If your class is derived from a non-serializable class, serialization can be achieved
by using method defaultWriteObject() in the ObjectOutputStream class, and can then be deserialized by method defaultReadObject() in class ObjectInputStream.
Once serialized, the object is ready to transmitted over a network. The following example demonstrates how to make an object serializable and how to send it through a stream socket:
//Object Output
import java.net.*;
import java.io.*;
//The sample class to be sent: Factory
class Factory implements Serializable
{ private void writeObject(ObjectOutputStream out) throws IOException
{ out.defaultWriteObject(); }
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{ in.defaultReadObject(); }
}
public class ShowObjOutput
{ public static void main(String[] arg)
{ try
{ ObjectOutputStream os;
Socket sock = new Socket("panda.cs.uno.edu", 6000);
//panda is the host name
Factory fa = new Factory();
os = new ObjectOutputStream( new
BufferedOutputStream(sock.getOutputStream()));
os.writeObject(fa);
}
catch (IOException ex)
{}
}
}
This next example shows you how an object is received by ObjectInputStream from a stream socket:
//Object Input
import java.net.*;
import java.io.*;
public class ShowObjInput
{ public static void main(String[] arg)
{ try
{ ObjectInputStream is;
ServerSocket servSock = new ServerSocket(6000);
Sock sock;
sock = servSock.accept();
is = new ObjectInputStream( new
BufferedInputStream(sock.getInputStream()));
Factory o = (Factory)is.readObject();
} catch (IOException ex)
{}
}
}
Besides tightly coupled sockets, Java also supports connectionless datagram communication with the DatagramSocket class. Can we achieve object input/output with datagram communication? Accomplishing this is not as straightforward as with
stream sockets. The problem is that the DatagramSocket is not attached to any stream; rather, a DatagramSocket takes a byte array as parameter for sending and receiving.