-
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
-
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{}
-
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
-
By Irina in forum ASP.NET
Replies: 0
Last Post: 11-29-2002, 10:47 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Roseta in forum VB Classic
Replies: 0
Last Post: 11-14-2001, 03:24 AM
-
By Ayman in forum VB Classic
Replies: 0
Last Post: 04-03-2000, 01:08 AM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks