Wednesday, September 5, 2012

where Protocol of serialization is defined in java..??

Many of you used Serializable interface but have you ever think where the protocol of saving Serialization object is defned?
Today i share my knowledge about it how much i know

first lest go through the process of saving object.
class saveObject implements Serializable
{
public static void main(String args[])
    {
    saveObject sb=new saveObject();
    FileOutputStream fo=new FileOutputStream(fileName);
    ObjectOutputStream obs=new ObjectOutputStream(fo);
    obs.writeObject(sb);
    }
}
now if saveObject class implements Serializable then only it work else it throw exception saveObject is not Serialized
this protocol is define in inside ObjectOutputStream.writeObject(Object) method
lets check the method what it have
public final void writeObject(Object obj) throws IOException {
    if (enableOverride) {
        writeObjectOverride(obj);
        return;
    }
    try {
        writeObject0(obj, false);
    } catch (IOException ex) {
        if (depth == 0) {
        writeFatalException(ex);
        }
        throw ex;
    }
    }
here enableOverride is boolean type whose value get assign false inside ObjectOutputStream(fo) constructor
so  writeObject0(obj, false); get execute
now lets see what in  writeObject0(obj, false) method

    private void writeObject0(Object obj, boolean unshared)
    throws IOException
    {
    boolean oldMode = bout.setBlockDataMode(false);
    depth++;
    try {
        // handle previously written and non-replaceable objects
        int h;
        if ((obj = subs.lookup(obj)) == null) {
        writeNull();
        return;
        } else if (!unshared && (h = handles.lookup(obj)) != -1) {
        writeHandle(h);
        return;
        } else if (obj instanceof Class) {
        writeClass((Class) obj, unshared);
        return;
        } else if (obj instanceof ObjectStreamClass) {
        writeClassDesc((ObjectStreamClass) obj, unshared);
        return;
        }
       
        // check for replacement object
        Object orig = obj;
        Class cl = obj.getClass();
        ObjectStreamClass desc;
        for (;;) {
        // REMIND: skip this check for strings/arrays?
        Class repCl;
        desc = ObjectStreamClass.lookup(cl, true);
        if (!desc.hasWriteReplaceMethod() ||
            (obj = desc.invokeWriteReplace(obj)) == null ||
            (repCl = obj.getClass()) == cl)
        {
            break;
        }
        cl = repCl;
        }
        if (enableReplace) {
        Object rep = replaceObject(obj);
        if (rep != obj && rep != null) {
            cl = rep.getClass();
            desc = ObjectStreamClass.lookup(cl, true);
        }
        obj = rep;
        }

        // if object replaced, run through original checks a second time
        if (obj != orig) {
        subs.assign(orig, obj);
        if (obj == null) {
            writeNull();
            return;
        } else if (!unshared && (h = handles.lookup(obj)) != -1) {
            writeHandle(h);
            return;
        } else if (obj instanceof Class) {
            writeClass((Class) obj, unshared);
            return;
        } else if (obj instanceof ObjectStreamClass) {
            writeClassDesc((ObjectStreamClass) obj, unshared);
            return;
        }
        }
       
        // remaining cases
        if (obj instanceof String) {
        writeString((String) obj, unshared);
        } else if (cl.isArray()) {
        writeArray(obj, desc, unshared);
        } else if (obj instanceof Enum) {
        writeEnum((Enum) obj, desc, unshared);
        } else if (obj instanceof Serializable) {        //check this line
        writeOrdinaryObject(obj, desc, unshared);
        } else {
        throw new NotSerializableException(cl.getName());
        }
    } finally {
        depth--;
        bout.setBlockDataMode(oldMode);
    }
    }

as per different condition it perform the action i will not go in detail .Just go through the code where i comment check this line
here our condition get satisfied only if our class implements Serializable interface else it throw exception NotSerializableException
if obj not belong to Enum,String ,Class,ObjectStreamClass.
writeOrdinaryObject(obj, desc, unshared); do the rest thing of saving object
 
For reading object near about  same concept happened in
ObjectInputStream obi=new ObjectInputStream(new FileOutputStream(fileName));
obi.readObject()