Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization Query

 
Deb Dey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there !

Have a query about serialization. Can someone please help...

If a serializable subclass inherits from a non serializable superclass can we serialize the inherited superclass member variable? I know in the normal case the superclass variable will not be serialized and when deserializing we will get the default value of the data member, but is it possible to have my own writeObject and readObject in the serializable subclass and save the superclass member using writeInt() and read the same by readInt(); I mean the same way we do it for a non serializable class member with a HAS-A relationship.

Thanks,
Deb
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deb, Welcome to javaranch.

What you are saying looks fine to me as far as I can understand it...
 
Deb Dey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

I was trying to make it work, but wid no luck

Here I am pasting the code that I was trying. It compiles fine but throws runtime exception. Can you help me see the anomaly, the exception log was not particularly helpful-

/* this uses one object(miao) for serialization
and another object (miao2) for deserialization

Plus we will try to save value of Animal's member: age*/

import java.io.*;

//Non serializable Class
class Animal
{
int age;
}


//Serializable Class
class PetAnimal extends Animal implements Serializable
{
int tailSize;
String name;

PetAnimal(int age, int tailSize, String name)
{ this.age=age;
this.tailSize=tailSize;
this.name=name;
}

//My method for serialization
private void writeObject(ObjectOutputStream os)
{
try
{
os.defaultWriteObject();
System.out.println("defaultWrite successful");
os.writeInt(age);
System.out.println("writeInt successful");
os.close();

}
catch (Exception e)
{
System.out.println("Exception in writeObject");
e.printStackTrace();
}
}


//My method for de-serialization
private void readObject(ObjectInputStream ois)
{
try
{
ois.defaultReadObject();
age= ois.readInt();
ois.close();

}
catch (Exception e)
{
System.out.println("Exception in readObject");
e.printStackTrace();
}
}
}


//Class that serializes
class MyHouse4
{
public static void main(String[] args){
PetAnimal miao=new PetAnimal(1, 2,"cat");

System.out.println("Before Serialization: " + miao.age+" "+miao.tailSize+" "+miao.name);
try{
FileOutputStream fs=new FileOutputStream("PetAnimal.ser");
System.out.println("File is fine");
ObjectOutputStream os= new ObjectOutputStream(fs);
System.out.println("object stream created is fine");
os.writeObject(miao);
System.out.println("object serialized");
os.close();
}
catch (Exception e)
{
System.out.println("Exception in serializing");
e.printStackTrace();

}

//Now deserialize
NewHouse house= new NewHouse();
house.deSerialize();
}
}


//Class that deserializes
class NewHouse
{
PetAnimal miao2=null;
void deSerialize()
{
try{
FileInputStream fis=new FileInputStream("PetAnimal.ser");
ObjectInputStream ois= new ObjectInputStream(fis);
miao2 = (PetAnimal) ois.readObject();
ois.close();
}
catch (Exception e)
{
System.out.println("Exception in de-serializing");
e.printStackTrace();
}
System.out.println("After de-serialization: " + miao2.age+" "+miao2.tailSize+" "+miao2.name);
}
}




The exception log-
C:\Program Files\Java\jdk1.5.0\bin>java MyHouse4
Before Serialization: 1 2 cat
File is fine
object stream created is fine
defaultWrite successful
writeInt successful
Exception in serializing
java.io.IOException: No such file or directory
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputSt
ream.java:1676)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Obj
ectOutputStream.java:1585)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:
1167)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:112
1)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav
a:1278)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)

at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.jav
a:1392)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:294)
at MyHouse4.main(MyHouse4.java:78)
Exception in de-serializing
java.io.IOException: No such file or directory
at java.io.FileInputStream.read(Native Method)
at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java
:2200)
at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream
.java:2490)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputSt
ream.java:2500)
at java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1865)

at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1839)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
713)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
at NewHouse.deSerialize(MyHouse4.java:105)
at MyHouse4.main(MyHouse4.java:91)
Exception in thread "main" java.lang.NullPointerException
at NewHouse.deSerialize(MyHouse4.java:113)
at MyHouse4.main(MyHouse4.java:91)

Thanks,
Deb
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your error is that your closing the streams inside the implementations of writeObject and readObject. I think you have to re-implement those functions in an exact way.

//try/catch
//invoke default read/write
//read/write extra vars
 
Mumtaz Khan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deb,
you are closing the os and is stream twice, you should not be closing them in your implementation of writeObject(ObjectOutputStream os) and readObject(ObjectInputStream is). remove it and your code will work fine.
 
Deb Dey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup its working now !

Thanks everyone ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic