从套接字使用ByteArrayInputStream接收byte [] [英] Receive byte[] using ByteArrayInputStream from a socket

查看:181
本文介绍了从套接字使用ByteArrayInputStream接收byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码,但收到错误:

Here is the code but got error:

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

是否可以接收 byte [] 从套接字使用 ByteArrayInputStream

Is it possible to receive byte[] using ByteArrayInputStream from a socket?

推荐答案

否。当你有一个字节数组时,你使用 ByteArrayInputStream ,并且你想要从数组中读取它就像它是一个文件一样。如果您只想从套接字读取字节数组,请执行以下操作:

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);

变量 count 将包含实际读取的字节数,当然数据将在数组 data 中。

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

这篇关于从套接字使用ByteArrayInputStream接收byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆