• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom Serialization

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody please help me with this code .
i am implementing readObject()/writeObject().
Trying to reading a String in ObjectInputStream. I tried different ObjectInputStream methods

readLine()->depracated ,Gives my o/p string along wih cryptic characters.
readLine() reads UTF so doesnt work,since i am readng plain file
readObect()->then casting to String gives exception .


import java.io.*;

/// non serializable class ,Instance as transient in Plant class
//---------------giving default values by using custom-serialization--
class Flower {
String flower;

Flower() {
System.out.println("i am flower");
flower = "dalia";
}

Flower(String s) {
flower = s;
}

String getFlowerValue() // putting some default values for transient class
// instance
{
return flower;
}

}

// ---------------------serializable class ,Instance(not transient) in Plant
// class
class Fruit1 implements Serializable {
String fruit;

Fruit1() {
System.out.println("i am fruit");
fruit = "mango";
}
}

// /-------------Serializable class------------- using objects of Fruit &
// Flower(transient)//
class Plant implements Serializable {
transient Flower myFlower; // /non serializable

Fruit1 myFruit;

Plant() {
System.out.println("Constructor:i am a plant wih flower & veg");
myFlower = new Flower();
myFruit = new Fruit1();
}

// custom serialization ----------------------
// implmenting methods --to write default Flower values with plant object

private void writeObject(ObjectOutputStream oos) {
try {
oos.defaultWriteObject();
System.out
.println("Explicitly writing Default Flower Object-Values: "
+ myFlower.getFlowerValue());
oos.writeChars(myFlower.getFlowerValue());

} catch (IOException e) {
System.out.println("Error in writing exolicitly");
}
}

// implementing readObject()
// Explicitly reading Flower
private void readObject(ObjectInputStream ois) {
System.out.println("----------------Here is BUG--------------");
try {
ois.defaultReadObject(); // normal object reading
String s = (String) ois.readObject();
// String-reading ,falls to IOException
// readLine() gives o/p but not proper,readUTF() -doesnt work

System.out.println("Checking String:->" + s);
myFlower = new Flower(s);
System.out.println(myFlower.flower);
ois.close();

} catch (ClassNotFoundException e) {
System.out.println("class not foud " + e);
}

catch (IOException e) {
System.out.println("IO inside readObject() Error " + e);
e.printStackTrace();
}
}

}

// /------------------------main class------------serializing................
public class SerialDemo4 {
public static void main(String args[]) {
Plant saved_plant = new Plant();
System.out.println("Plant object created ");
try {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("Plants.ser"));

oos.writeObject(saved_plant);

oos.close();
System.out.println("Plant objects written with Fruit ");
System.out
.println("------------------------------------------------------------------");

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"Plants.ser"));
Object obj = ois.readObject();
Plant recov_plant = (Plant) obj;
// Plant plant1=(Plant)ois.readObject();

ois.close();

System.out.println("objects recovered-> " + recov_plant + " "
+ recov_plant.myFlower.flower + " "
+ recov_plant.myFruit.fruit);
// myFlower(transient) --doesnt work, myFruit-workss
} catch (IOException e) {
System.out.println("Main Program:IO error " + e);
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("Main Program:Class not found while reading "
+ e);
}
}
}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use the writeChars method in ObjectOutputStream, the String that you send as a parameter is written to the stream as a sequence of chars.

When you try to read in those chars with the method readObject(), then you are going to get an OptionalDataException because char is a primitive.

One way to fix the problem would be to write the String using the writeObject() method. Then you would be able to use the readObject() method and cast the output to a String.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did make the change .
Instead of writeChars() ,i used writeObject() ,OptionalDataException is not coming but ,now it started given readObject() excpetion in main class.

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"Plants.ser"));
Object obj = ois.readObject();
Plant recov_plant = (Plant)obj;
// Plant plant1=(Plant)ois.readObject();

this readObject() is throwing IOException .but private readOBject() is correct ,i suppose.

thnx
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i found the bug , sorry to bother you .It was ois.close() in private readObject() ,i have removed it in local method & it worked
thanks
 
This is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic