+92 332 4229 857 99ProjectIdeas@Gmail.com

Serialization In Java


Want to send an object to a stream, to some file or somewhere on network.

Object know how to read/write themselves to streams.

Serialization is also called as flattening, Streaming, Dehydrate (rehydrate = read), Archiving.

Serializable Interface:
                              By implementing this interface a class declares that is willing to  be       read/written by automatic serializable machinery. import java.io.*; for using serialization.

     Automatic Writing

System knows how to recursively write out the state of an object to stream

Recursively follows references and writes out those objects too!

    Automatic Reading

System knows how to read the data from Stream and re-create object in memory

Downcasting is required

public class PersonInfo implements Serializable {


    String name;

    String address;

    String phoneNum;


public void display( ){


JOptionPane.showMessageDialog(null, "name: " + name + "address:" + address + "phoneNum:"  + phoneNum);


  }

}

   public class SaveObject {


       public static void main(String[] args) {

             

       PersonInfo pWrite = new PersonInfo("Maryam", "Pakistan", "423472389");

      

try {          



               FileOutputStream fos   = new FileOutputStream("newFile.dat");

               ObjectOutputStream out = new ObjectOutputStream(fos);



               //serialization

               out.writeObject(pWrite); 



               out.close();

               fos.close();



           } catch (Exception ex){

              ex.printStackTrace();

       } 

}
}



   public class ReadObject {


      public static void main(String[] args) {

      

       try {          



            FileInputStream fis = new FileInputStream("newFile.dat");

            ObjectInputStream in = new ObjectInputStream(fis);



            //de - serialization

            PersonInfo pRead = (PersonInfo) in.readObject(); 



            pRead.printPersonInfo();



            in.close();

            fis.close();



        } catch (Exception ex){

           ex.printStackTrace();

        }

   }

 }

Sending Objects over network:

You can read/write objects to network using sockets (import java.net.*;)

The class version should be same on both sides (client & Server) of the network

      PersonInfo p = new PersonInfo("Neelam", "Pakistan", "423472389");

    Socket s = new Socket("localhost",222);

    OutputStream os = s.getOutputStream();

    ObjectOutputStream oos = new ObjectOutputStream(os);

    oos.writeObject(p);

Reading Objects from network:

ServerSocket ss = new ServerSocket();//ServerSocket(int portNumber)

Socket s = ss.accept();

InputStream in = s.getInputStream();

ObjectInputStream ois = new ObjectInputStream(in);

PersonInfo p = (PersonInfo)ois.readObject();

Preventing Serialization:
transient keyword is used to mark a field that should not be serialized

Often there is no need to serialize sockets, streams & DB connections etc. (they do not represent the state of object, rather connections to external resources)

So, we can mark them as:


        public transient Socket s;

        public transient OutputStream os;
        public transient Connection con;



transient fields are returned as null on reading

public class PersonInfo implements Serializable {

    String name;
    String address;
    transient String phoneNum;


public void display(){


JOptionPane.showMessageDialog(null, "name: " + name + "address:" + address + "phoneNum:"  + phoneNum);

       }
}

0 comments: