Java ObjectStream Class with Examples
Object streams, like data streams, allow for the I/O of primitive data types. Most, but not all, standard classes enable their objects to be serialized. Those that do implement the Serializable marker interface.
ObjectInputStream and ObjectOutputStream are the object stream classes. These classes implement ObjectInput and ObjectOutput, which are DataInput and DataOutput subinterfaces.
That is, all of the primitive data I/O methods covered in Data Streams are also implemented in object streams. As a result, an object stream can contain both primitive and object values. This is demonstrated by the ObjectStreams example.
With a few exceptions, ObjectStreams creates the same application as DataStreams. To begin with, prices are now BigDecimalobjects to represent fractional values better. Then, a Calendar object is written in the data file.
Complex Object Output and Input
The writeObject and readObject methods are simple to use, but they contain complex object management logic. This is unimportant for a class like Calendar, which only holds primitive values.
Many objects, however, contain references to other objects. If readObject is to recreate an object from a stream, it must be able to recreate all the objects to which the original object referred.
These additional objects may have their references, for example. WriteObject traverses the entire web of object references and writes all objects in that web to the stream.
As a result, a single call to writeObject can result in many objects being written to the stream.
What happens if two objects on the same stream both contain references to the same object? Will they both refer to the same object when read back? Yes, the answer is “yes.” A stream can only have one copy of an object, but it can have unlimited references to it. As a result, if you explicitly write an object to a stream twice, you’re only writing the reference twice.
For instance, consider the following code, which writes an object ob twice to a stream:
Object ob = new Object(); out.writeObject(ob); out.writeObject(ob); Because each writeObject must be matched by a readObject, the code that reads the stream back will look like this:
Object ob1 = in.readObject(); Object ob2 = in.readObject();
This yields two objects.
ObjectInputStream Operation The ObjectInputStream is primarily used to read data written by the ObjectOutputStream.
Essentially, the ObjectOutputStream converts Java objects into streams. This is referred to as serialization. These converted streams can be saved to files or transmitted over networks.
If we need to read those objects, we’ll use the ObjectInputStream to convert the streams back to objects. This is referred to as deserialization.
Construct an ObjectInputStream:
We must first import the java.io.ObjectInputStream package to create an object input stream. Here’s how to make an input stream after we’ve imported the package.
//Creates a file input stream linked with the specified file FileInputStream fileStream = new FileInputStream(String file); // Creates an object input stream using the file input stream ObjectInputStream objStream = new ObjectInputStream(fileStream);
ObjectInputStream methods:
The ObjectInputStream class implements the InputStream class’s methods.
Method read()
read() returns a single byte from the input stream.
readBoolean() is a function that reads boolean data.
readChar() – reads data in character format.
readInt() is a function that reads integer data.
readObject() is a function that retrieves an object from the input stream.
Example:
import java.io.ObjectStreamClass;
import java.util.Calendar;
public class TechVidvan{
public static void main(String[] args) {
ObjectStreamClass osc = ObjectStreamClass.lookup(String.class); System.out.println("String in TechVidvan: " + osc.getField("class"));
ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);
System.out.println("Calendar in TechVidvan: " + osc2.getField("class"));
}
}
Output
String in TechVidvan: null
Calendar in TechVidvan: null
Complex Object Output and Input
The writeObject and readObject methods are simple to use, but they contain complex object management logic. This is unimportant for a class like Calendar, which only holds primitive values. Many objects, however, include references to other objects. If readObject is to recreate an object from a stream, it must be able to recreate all the objects to which the original object referred. These additional objects may have their references.
For example. WriteObject traverses the entire web of object references and writes all objects in that web to the stream. As a result, a single call to writeObject can result in many objects being written to the stream. What happens if two objects on the same stream both contain references to the same object? Will they both refer to the same object when read back? Yes, the answer is “yes.” A stream can only have one copy of an object, but it can have unlimited references to it. As a result, if you explicitly write an object to a stream twice, you’re only writing the reference twice.
For instance, consider the following code, which writes an object ob twice to a stream:
Object ob = new Object(); out.writeObject(ob); out.writeObject(ob); Because each writeObject must be matched by a readObject, the code that reads the stream back will look like this:
Object ob1 = in.readObject(); Object ob2 = in.readObject();
This yields two objects.
ObjectOutputStream named output using the FileOutputStream named file
ObjectInputStream named input using the FileInputStream named fileStream
A Dog class object dog
We then used the object output stream to write the object to the file. The object input stream is used to read the object from the file.
The Dog class implements the Serializable interface. This is because the ObjectOutputStream only writes serializable objects to the output stream.
Conclusion
Java streams support functional-style operations on element streams. A stream is a non-mutable collection of functions applied to data in a specific order. A stream is not a collection of elements that can be stored.
