Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java serialization, ObjectInputStream.readObject(), check if will block

I'm using an ObjectInputStream to call readObject for reading in serialized Objects. I would like to avoid having this method block, so I'm looking to use something like Inputstream.available().

InputStream.available() will tell you there are bytes available and that read() will not block. Is there an equivalent method for seriailzation that will tell you if there are Objects available and readObject will not block?

like image 768
Mike Avatar asked Oct 01 '09 00:10

Mike


2 Answers

No. Although you could use the ObjectInputStream in another thread and check to see whether that has an object available. Generally polling isn't a great idea, particularly with the poor guarantees of InputStream.available.

like image 166
Tom Hawtin - tackline Avatar answered Oct 05 '22 16:10

Tom Hawtin - tackline


The Java serialization API was not designed to support an available() function. If you implement your own object reader/writer functions, you can read any amount of data off the stream you like, and there is no reporting method.

So readObject() does not know how much data it will read, so it does not know how many objects are available.

As the other post suggested, your best bet is to move the reading into a separate thread.

like image 31
David Crawshaw Avatar answered Oct 05 '22 17:10

David Crawshaw