Java — How to read an unknown number of bytes from an inputStream (socket/socketServer)?

后端 未结 11 1002
梦如初夏
梦如初夏 2020-12-15 07:42

Looking to read in some bytes over a socket using an inputStream. The bytes sent by the server may be of variable quantity, and the client doesn\'t know in advance the lengt

11条回答
  •  独厮守ぢ
    2020-12-15 08:14

    Use BufferedInputStream, and use the available() method which returns the size of bytes available for reading, and then construct a byte[] with that size. Problem solved. :)

    BufferedInputStream buf = new BufferedInputStream(is);  
    int size = buf.available();
    

提交回复
热议问题