0

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!

1
  • 1
    It doesn't add a byte array, it adds the 44th value of an array Commented Feb 27, 2015 at 8:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I undestand that and it is the correct answer on my question. But how does that work? Adding a byte to an integer?
The only difference between byte and integer is that a byte is one byte long (-128 to 127), and an int is 4 bytes long (-2,147,483,648 to 2,147,483,647). The byte gets converted into an integer and then they are added.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.