Java InputStream.read(byte[], int, int) method, how to block until the exact number of bytes has been read

前端 未结 4 519
悲&欢浪女
悲&欢浪女 2021-02-04 11:53

I\'m writing a simple client/server network application that sends and receives fixed size messages through a TCP socket.

So far, I\'ve been using the <

4条回答
  •  不思量自难忘°
    2021-02-04 12:07

    I think the correct version of ratchet freak's answer is this :

    for (int index = 0; index < toRead && (read = inputStream.read(bytes, index, toRead-index))>0 ; index+= read);
    

    it stops reading if read returns -1

提交回复
热议问题