ObjectInputStream available() method doesn't work as expected (Java)

后端 未结 4 1017
时光说笑
时光说笑 2021-01-07 11:00

I\'ve been trying to figure out why a method I\'ve written to read objects from a file didn\'t work and realized that the available() method of ObjectInputStream gave 0 even

4条回答
  •  迷失自我
    2021-01-07 11:31

    Because, as the javadoc tells, available() returns an estimation of the number of bytes that can be read without blocking. The base InputStream implementation always returns 0, because this is a valid estimation. But whatever it returns, the fact that it returns 0 doesn't mean that there is nothing to read anymore. Only that the stream can't guarantee that at least one byte can be read without blocking.

提交回复
热议问题