java.io.EOFException when try to read from a socket

后端 未结 4 615
[愿得一人]
[愿得一人] 2020-12-19 12:02

i don\'t know why java.io.EOFException appear. i want to write a file after i get binary stream from server.

Here\'s my code

inputSt         


        
4条回答
  •  感情败类
    2020-12-19 12:26

    DataInputStream.readByte API does not say it return -1 on EOS, it says

    Returns:the next byte of this input stream as a signed 8-bit byte.

    Throws: EOFException - if this input stream has reached the end.

    It assumes that when working withh DataInputStream.readByte we know how many bytes are left in the stream. Otherwise we can use EOFException as an indicator of EOS.

    BTW If you use read() you will get -1 on EOS without EOFException

提交回复
热议问题