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

前端 未结 4 1771
别跟我提以往
别跟我提以往 2020-12-03 16:14

I\'m using an ObjectInputStream to call readObject for reading in serialized Objects. I would like to avoid having this method block,

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 16:49

    The BufferedInputStream works for me, and why not just check if(bis.available() > 0) instead of a N value, this works perfectly for me. I think ObjectInputStream.readObject blocks(= waits until) when no input is to be read. So if there is any input at all in the stream aka if(bis.available() > 0) ObjectInputStream.readObject will not block. Keep in mind that ObjectInputStream.readObject might throw a ClassNotFoundException, and that is't a problem at all to me.

提交回复
热议问题