Java Reference
In-Depth Information
What Is HTTP Tunneling?
HTTP tunneling is a method of reading and writing serialized objects using an HTTP connec-
tion. You are creating a sub-protocol inside the HTTP protocol—that is, “tunneling” inside
another protocol. This relieves you from the hassles of dealing with the actual transport layer.
Object Serialization
Before you can get started with HTTP tunneling, you need to understand object serialization.
Object serialization
is a feature introduced in the Java Development Kit version 1.1. It enables
you to create objects that are persistent across several media. An exceptionally convenient
characteristic of serialization is that it not only serializes your object, it also unwinds and seri-
alizes all the objects that are referenced by your object.
To make an object serializable, it must implement the
Serializable
interface. This interface is
found in the
java.io
package. The entire
Serializable
interface is listed as follows:
public interface Serializable {
static final long serialVersionUID = 1196656838076753133L;
}
As you can see, there is not much to this interface. Its only purpose is to signify that an object
can be serialized.
The steps involved in serializing an object are as follows:
1.
Create an
OutputStream
object. This can be a stream to a file, a TCP/IP connection, or
most any other target.
2.
Create an
ObjectOutputStream
and pass to its constructor the
OutputStream
created in
Step 1.
3.
Call the
ObjectOutputStream
's
writeObject()
method and pass to it the object that
implements the
Serializable
interface.
The equivalent steps involved in reading a serialized object are as follows:
1.
Create an
InputStream
object that points to the location of the serialized object.
2.
Create an
ObjectInputStream
and pass to its constructor the
InputStream
created in
Step 1.
3.
Call the
ObjectInputStream
's
readObject()
method.
4.
The
readObject()
method returns an upcasted
Object
that must be downcasted back to
your original object's type.