简体   繁体   English

DataInputStream在Android中仅接收2048字节的数据吗?

[英]Does DataInputStream receives only 2048 bytes of data in Android?

From PC (server side) the C#.Net application has to sent 22000 bytes of data to an Android device (client side) via Wi-Fi. 从PC(服务器端)开始,C#.Net应用程序必须通过Wi-Fi将22000字节的数据发送到Android设备(客户端)。 But dataInputStream in Android device is showing only 2048 bytes of it. 但是Android设备中的dataInputStream仅显示其中的2048个字节。

dataInputStream = new DataInputStream(workerSocket.getInputStream());
byte[] rvdMsgByte = new byte[dataInputStream.available()];
for (int i = 0; i < rvdMsgByte.length; i++)
    rvdMsgByte[i] = dataInputStream.readByte();
String rvdMsgStr = new String(rvdMsgByte);

I am confused with the following: 我对以下内容感到困惑:

  1. Is PC can send only 2048 bytes of data? PC是否只能发送2048字节的数据?
  2. Or, is Android device has only 2048 bytes of capacity to receive data? 或者,Android设备是否只有2048字节的容量来接收数据?
  3. Or, Does dataInputStream shows only 2048 bytes even after device received all bytes? 或者,即使设备接收到所有字节后, dataInputStream是否也dataInputStream显示2048个字节?

    If (data_received <= 2048 bytes) Above code works perfect; 如果(data_received <= 2048字节)上面的代码可以正常工作;

You basically shouldn't be using InputStream.available() . 基本上,您不应该使用InputStream.available() That tells you how much data is available right now - which may well not be the whole message, if it's still coming over the network. 这告诉你有多少数据是可用的,现在 -这很可能不是整条消息,如果它仍然在网络上到来。

If the stream will end at the end of the message, you should simply keep looping, reading a block at a time until you're done (preferrably not just reading a byte at a time!). 如果流将在消息末尾结束,则您应该简单地继续循环,一次读取一个块,直到完成为止(最好只是一次读取一个字节!)。 For example: 例如:

byte[] readFully(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[8 * 1024]; // 8K buffer
    int bytesRead;
    while ((bytesRead = input.read(buffer)) > 0) {
        output.write(buffer, 0, bytesRead);
    }
    return output.toByteArray();
}

If you need to break your stream up into messages, you should consider how to do that - sending a "message length" prefix before the data is usually the best option. 如果需要将流分解为消息,则应考虑如何做-在数据通常是最佳选择之前发送“消息长度”前缀。

Once you've read all the data, you should not do this: 一旦你阅读所有的数据,你应该这样做:

String rvdMsgStr = new String(rvdMsgByte);

That will use the platform default encoding to convert the bytes into text. 这将使用平台默认编码将字节转换为文本。 If this is genuinely text data, you should use the string constructor overload which lets you specify the encoding explicitly. 如果这是真正的文本数据,则应使用字符串构造函数重载,该重载可让您显式指定编码。

If it's not genuinely text data, you shouldn't be trying to treat it as such. 如果不是真正的文本数据,则不应尝试将其视为文本数据。 If you really need to represent it as a string, use base64. 如果确实需要将其表示为字符串,请使用base64。

Also note that there's no need to use DataInputStream here, as far as I can tell. 另外请注意,据我所知,这里不需要使用DataInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM