Here is the code but got error:
bin = new ByteArrayInputStream(socket.getInputStream());
Is it possible to receive byte[]
using ByteArrayInputStream
from a socket?
Example: ByteArrayInputStream to read data ByteArrayInputStream input = new ByteArrayInputStream(array); Here, the input stream includes all the data from the specified array. To read data from the input stream, we have used the read() method.
You can simply iterate the byte array and print the byte using System. out. println() method.
A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method. Closing a ByteArrayInputStream has no effect.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With