Serialization is the process of converting the state of an object into byte stream. De-serialization allow you to restore the serialized state into another instance of the same type. As you know, only the state of an object can serialized. Behavior is run-time phenomenon and it can’t be converted to a stream. Classes java.io.ObjectOutputStream and java.io.ObjectInputStream are provide necessary APIs to serialize and deserialize an object.
Serialization
In general, there are three steps involved in serializing a java object.
- Marking the class as Serializable
- Create an instance of ObjectOutputStream
- Use writeObject method of ObjectOutputStream to write the object into stream
Marking the class as Serializable
It just informs the compiler that this java class may be serialized. java.io.Serializable is a marker interface.
1 |
class Laptop implements java.io.Serializable{} |
Create an instance of java.io.ObjectOutputStream
1 2 |
ObjectOutputStream objectStream = new ObjectOutputStream(new FileOutputStream( new File(<<file_name>>))); |
Write object to underlying output stream
1 2 3 |
ObjectOutputStream objectStream = new ObjectOutputStream(new FileOutputStream( new File(<<file_name>>))); objectStream.writeObject(object); |
Important API to serialize an object
1 2 |
Class Name : java.io.ObjectOutputStream Method Name : public final void writeObject(Object x) throws IOException |
Deserialization
Deserialization is process of restoring the serialized state of an object. In general, there are three steps involved in deserialization.
- Load the serialized state as stream
- Create an object of the java.io.ObjectInputStream with stream loaded in step one
- Read the object and cast to correct type
Load the serialized state as stream
1 |
final FileInputStream inputStream = new FileInputStream(new File(<<file_name>>)); |
Create an object of the ObjectInputStream with stream loaded in step one
1 |
final ObjectInputStream objectStream = new ObjectInputStream(inputStream); |
Read the object and cast to correct type
1 2 |
final ObjectInputStream objectStream = new ObjectInputStream(inputStream); final Laptop laptop = (Laptop)objectStream.readObject(); |
Important API to Deserialize an object
1 2 |
Class Name : java.io.ObjectInputStream Method Name : public final Object readObject() throws IOException |
Complete Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.ourownjava.corejava.io; import static org.junit.Assert.assertEquals; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.junit.Before; import org.junit.Test; /** * * @author ourownjava.com * * Example program for java serialization and de-serialization * */ public class SerializationBehavior { private File file; private Laptop laptop; @Before public void setUp(){ file = new File("/home/thosan/java/testfiles/laptop"); laptop = new Laptop(); laptop.setMake("Dell"); laptop.setPrice(1000.10); laptop.setPrimaryMemorySize(4056); } @Test public void shouldSerializeLaptopObject() throws FileNotFoundException, IOException{ file.delete(); file.createNewFile(); assertEquals(0, file.length()); final ObjectOutputStream objectInputStream = new ObjectOutputStream(new FileOutputStream(file)); objectInputStream.writeObject(laptop); assertEquals(129, file.length()); objectInputStream.close(); } @Test public void shouldDeserializeLaptopObject() throws FileNotFoundException, IOException, ClassNotFoundException{ final ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file)); Laptop laptop = (Laptop) objectInputStream.readObject(); assertEquals("Dell", laptop.getMake()); assertEquals(Double.valueOf(1000.10), Double.valueOf(laptop.getPrice())); assertEquals(4056, laptop.getPrimaryMemorySize()); objectInputStream.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.ourownjava.corejava.io; import java.io.Serializable; /** * * @author ourownjava.com * */ public class Laptop implements Serializable{ private static final long serialVersionUID = -1; private String make; private int primaryMemorySize; private double price; public String getMake() { return make; } public void setMake(String make) { this.make = make; } public int getPrimaryMemorySize() { return primaryMemorySize; } public void setPrimaryMemorySize(int primaryMemorySize) { this.primaryMemorySize = primaryMemorySize; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } |
Pingback: Serialization and Deserialization in java. | Clean Java