Receive byte[] using ByteArrayInputStream from a socket

后端 未结 2 1633
感动是毒
感动是毒 2021-01-02 06:56

Here is the code but got error:

bin = new ByteArrayInputStream(socket.getInputStream());

Is it possible to receive byte[] usin

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 07:44

    No. You use ByteArrayInputStream when you have an array of bytes, and you want to read from the array as if it were a file. If you just want to read arrays of bytes from the socket, do this:

    InputStream stream = socket.getInputStream();
    byte[] data = new byte[100];
    int count = stream.read(data);
    

    The variable count will contain the number of bytes actually read, and the data will of course be in the array data.

提交回复
热议问题