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

前端 未结 4 1777
别跟我提以往
别跟我提以往 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:39

    I have an idea that by adding another InputStream into the chain one can make availability information readable by the client:

    HACK!

    InputStream is = ... // where we actually read the data
    BufferedInputStream bis = new BufferedInputStream(is);
    ObjectInputStream ois = new ObjectInputStream(bis);
    
    if( bis.available() > N ) {
      Object o = ois.readObject();
    }
    

    The tricky point is value of N. It should be big enough to cover both serialization header and object data. If those are varying wildly, no luck.

提交回复
热议问题