Wednesday, February 9, 2011

Java serialization

Definition:
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment.[1] When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward.

This process of serializing an object is also called deflating or marshalling an object.[2] The opposite operation, extracting a data structure from a series of bytes, is deserialization (which is also called inflating or unmarshalling).
http://en.wikipedia.org/wiki/Serialization

Java Serialization:
Java object serialization is used to persist Java objects to a file, database, network, process or any other systems. Serialization flattens objects into an ordered, or serialized stream of bytes. The ordered stream of bytes can then be read at a later time, or in another environment, to recreate the original objects. For instance, you can create an object on Windows server, serialize it, and then send it to a Linux server to deserialize it without worrying about different byte order or OS details.

Serializing an object needs to store object itself, but also needs to store all references, so all referenced objects are serializable to avoid NotSerializableException.

Java serialization is mainly for two major usage: RMI (remote method invocation) and JavaBean persistence.

How to serialize Java object?
ObjectOutputStream is the primary output stream class that implements the ObjectOutput interface for serializing objects (using writeObject method). ObjectInputStream is the primary input stream class that implements the ObjectInput interface for deserializing objects (using readObject method).

Keywords:
  1. Serializable - tagging interface without method or attributes, to indicate the Java object is serializable. (
    writeReplace, readResolve, writeObject, readObject)
  2. Externalizable - sub-interface of Serializable (writeExternal, readExternal)
  3. transient - indicate the instance field not persistable (for security etc)
  4. serialVersionUID - for version control
Sample Codes:
        // serializing
        List<String> list = new ArrayList<String>();
        list.add("xxx");
        list.add("yyy");
        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try {
            fos = new FileOutputStream("c:\\filename.txt");
            out = new ObjectOutputStream(fos);
            out.writeObject(list);
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        // deserializing it
        FileInputStream fis = null;
        ObjectInputStream in = null;
        try {
            fis = new FileInputStream("c:\\filename.txt");           
            in = new ObjectInputStream(fis);
            list = (ArrayList<String>) in.readObject();
            in.close();
            for (String s : list) {
                System.out.println(s);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

No comments:

Post a Comment