Peter
Peter

Reputation: 393

Add ByteArray to integer

In the following java code-snippet you'll see this line packetLengthMax += bytes.toByteArray()[43]; My question is: How does this work?

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];
    }
}

My explanation: First a socket (Client) is passed to the code. Then it does the setup of all variables. In the while loop, it reads all data that comes from the socket. Then it also writes this data to the DataOutputStream. But in the if statement - it adds a byte array to an integer.
How does it work? I don't get that point. Thank you for helping!

Upvotes: 0

Views: 46

Answers (1)

Nicholas Daley-Okoye
Nicholas Daley-Okoye

Reputation: 2397

It's not adding the whole byte array, it's just adding the byte at position 43. (i.e. the 44th byte in the array).

Upvotes: 1

Related Questions