0

I have some input that I add to a serialized object.

Now when I read the serialized object, I want to check if it exists... If not loop till it has a value in it.

How do i modify the deserialization function to handle that.

There is basically a delay in populating my serializable object. So in the meantime if i were to read that object, it is going to be empty. I want to put a check to read only when it has data in it. if not it should wait till it has some data

public String _displayResults(){
String  SomeData = "";
try {
    FileInputStream fis = new FileInputStream("SomeDataobj");
    ObjectInputStream ois = new ObjectInputStream(fis);
    SomeData = (String)ois.readObject();
    ois.close();
}
catch(Exception e) {
    System.out.println("Exception during deserialization: ");
}
return SomeData;
}

What I tried:

added a wait condition for 2 secs for 10 times... Is there a cleaner way.

    while ( ois.readObject().toString().equalsIgnoreCase("") && i <10){
        Thread.sleep(2000);
        i++;
    }
Haran Murthy
  • 341
  • 2
  • 10
  • 30
  • 3
    What do you mean by "check if it exists"? It's *really* unclear what you're trying to do here. – Jon Skeet Oct 08 '12 at 16:50
  • Assuming there is a sens to "waiting for a file to get written, then read it", why implement the wait logic inside the deserialization method ? Why not have whatever calls that method implement it properly? – b2Wc0EKKOvLPn Oct 08 '12 at 16:56
  • ylabidi- The process is designed in way to fetch data and then populate the object.. Delay is imminent on the calling side... I need to handle it in the receiving end. – Haran Murthy Oct 08 '12 at 17:13
  • ois.readObject().toString().equalsIgnoreCase("") will never return true as long as object is not null( it gives you hashcode of the object which will not be empty). On other side, you will get NullPointerException if the object is null!! – Satheesh Cheveri Oct 08 '12 at 17:22
  • hey satheesh--- yes thats the problem. how do i resolve that – Haran Murthy Oct 08 '12 at 17:54
  • what you need actually is file change listener. Have a look at here; http://stackoverflow.com/questions/494869/file-changed-listener-in-java . simply getting events when something changes over your files. – Hayati Guvence Oct 08 '12 at 22:23

1 Answers1

0

Java provides an API called Externalizable, which allows you to customize the (de) serialization. Serialiazable is marker interface and that indicates the object can be wrote to output stream. Externalizable provides two methods readExternal() and writeExternal() where you can override the behavior.

Your question is not so clear about what you want to achieve, so I am not sure if the above information is helpful for you

Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46