Serialization Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Serialization Problem

  1. #1
    Join Date
    Sep 2005
    Posts
    10

    Serialization Problem

    Hi all,

    I am not understanding Serialization.When we say Serialization of objects,we mean writing the object in bytes format in streams.But in case of RMI ,we do not write the serializable object into streams.Is it handled by JVM
    internally or there is something else.Please express your views.

    I also want to know that whenever we serialize an object,every time its our responsibility to write the object into stream or it is handled by jvm or some other software usin this serializable object.

    with regards,
    ajse

  2. #2
    Join Date
    Dec 2005
    Posts
    19
    I suggest you look into the serializable API for further information

    As for the implementation, normally we serialize the Javabean classes (any classes with private class scope variables, public setters and getters methods). Then when you create objects from this class, it's already serializable. You do not have to implement any methods in this Serializable interface because there is none. This interface is just used for identifying this class is serializable or not. Serializable is meant to break data into smaller chunks for faster transmission, then on the receiving end, he chunks of data is grouped back again.

    example: public class JB implements Serializable{}

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    For standard serialization you must implement the readObject and writeObject
    methods as specified in the Serializable interface. You write the elements
    out using the ObjectOutputStream in the same order as you read them in
    using the ObjectInputStream. Thats all you have to do.

    Serialization is ideal for streaming data on the web, but its also very efficient
    if you have an application that has a (minor) need for something like a
    database. You get a lot of info into a megabyte of ram.

    Here is the "how to boil an egg" recipie.

    Code:
    import java.io.*;
    
    /**
     * A basic serialization of two classes
     */
    
    class MyClass implements Serializable {
      int num=0;
      String name=null;
      public MyClass(int num, String name) {
        this.num=num;
        this.name=name;
      }
      public String toString() {
        return num+" : "+name;
      }
      /**
       * The serialization of MyClass
       */
      private void writeObject(java.io.ObjectOutputStream out)
          throws IOException {
        out.writeInt(num);
        out.writeObject(name);
      }
      private void readObject(java.io.ObjectInputStream in)
          throws IOException, ClassNotFoundException {
        this.num=in.readInt();
        this.name=(String)in.readObject();
      }
    }
    
    class MyBigClass implements Serializable {
      String baseName=null;
      MyClass [] mc=null;
      public MyBigClass(int num, String baseName) {
        mc=new MyClass[num];
        this.baseName=baseName;
        for (int i=0; i<mc.length; i++) {
          mc[i]=new MyClass(i,baseName+"_"+i);
        }
      }
      public String toString () {
        StringBuffer sb=new StringBuffer();
        sb.append("baseName : "+baseName).append("\n");
        for (int i=0; i<mc.length; i++) {
          sb.append(mc[i].toString()).append("\n");
        }
        return sb.toString();
      }
      /**
       * The Serialization of MyBigClass
       */
      private void writeObject(java.io.ObjectOutputStream out)
          throws IOException {
        out.writeObject(baseName);
        /**
         * the entire array is serialized, using MyClass' serialization
         * for each element of the array, for free ...
         */
        out.writeObject(mc);
      }
      private void readObject(java.io.ObjectInputStream in)
          throws IOException, ClassNotFoundException {
        this.baseName=(String)in.readObject();
        /**
         * get the array back again by a simple typcast of the read Object
         */
        mc=(MyClass[])in.readObject();
      }
    }
    /**
     * A driver class
     */
    public class SerializeIt {
      MyBigClass mbc=null;
    
      public SerializeIt() {}
    
      public void serOut (String fileName) throws Exception {
        mbc=new MyBigClass(5,"TestData");
    
        FileOutputStream out=new  FileOutputStream(fileName);
        ObjectOutputStream oos=new ObjectOutputStream(out);
        oos.writeObject(mbc);
        oos.close();
        System.out.println("Data serialized to: "+fileName);
      }
      public void serIn (String fileName) throws Exception {
        FileInputStream in=new  FileInputStream(fileName);
        ObjectInputStream ois=new ObjectInputStream(in);
        mbc=(MyBigClass)ois.readObject();
        System.out.println("Data read from: "+fileName);
        System.out.println(mbc.toString());
      }
    
      public static void main(String[] args) {
        String base="c:\\tmp\\tst.dat";
        SerializeIt sr = new SerializeIt();
        try {
          sr.serOut(base);
          sr.serIn(base);
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    
    }
    eschew obfuscation

Similar Threads

  1. Problem with Search
    By Irina in forum ASP.NET
    Replies: 0
    Last Post: 11-29-2002, 10:47 PM
  2. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  3. a problem with font and language
    By Roseta in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 03:24 AM
  4. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 0
    Last Post: 04-03-2000, 01:08 AM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center