Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java reading different variables with ObjectInputStream

I have an ObjectInputStream which needs to read two different inputs which are a String and my own created object. I have a thread which constantly waits for an input and depending on the input be it a string or object it will process the result. I need a way for the input to be able to distinguish between the two.

Any help would be great.

Thanks

like image 865
bubblebath Avatar asked May 30 '26 22:05

bubblebath


1 Answers

Isn't it a case of doing:

if (objectFromStream instanceof YourObejct) {

    YourObject obj = (YourObject) objectFromStream;
    ....

} else if (objectFromStream instanceof String) {

    String str = (String) objectFromStream;

} else {
  // throw excepption..

}
like image 137
Richard H Avatar answered Jun 01 '26 11:06

Richard H