0

在下面的 java 代码片段中,您将看到这一行packetLengthMax += bytes.toByteArray()[43]; 我的问题是:这是如何工作的?

byte[] dataBuffer = new byte[265];
int packetLength = 0;
int packetLengthMax = 44;
ByteArrayOutputStream   bytes       = new ByteArrayOutputStream();
DataOutputStream        outMessage  = new DataOutputStream(bytes);
/* Client = Socket*/
DataInputStream         clientIn    = new DataInputStream(Client.getInputStream());
while (packetLength < packetLengthMax) {
    packetLength += clientIn.read(dataBuffer);
    outMessage.write(dataBuffer);           
    if (packetLength >= 43) {
        packetLengthMax += bytes.toByteArray()[43];
    }
}

我的解释:首先将一个套接字 ( Client) 传递给代码。然后它会设置所有变量。在 while 循环中,它读取来自套接字的所有数据。然后它还会将此数据写入DataOutputStream. 但在if语句中 - 它将一个字节数组添加到一个整数。
它是如何工作的?我不明白这一点。感谢您的帮助!

4

1 回答 1

1

它不是添加整个字节数组,它只是在位置 43 处添加字节。(即数组中的第 44 个字节)。

于 2015-02-27T08:05:13.400 回答