1

I am sending byte arrays over a socket. The sent data starts off with 4 bytes indicating the length of the following byte array.

// get the amount of data being sent
byte[] lengthOfReplyAsArray = new byte[4];
forceRead(inputStream, lengthOfReplyAsArray);
int lengthOfReply = byteArrayToInt(lengthOfReplyAsArray);

// read the data into a byte array
byte[] reply = new byte[lengthOfReply];
forceRead(inputStream, reply);

The method used to read data from an InputStream:

private byte[] forceRead(InputStream inputStream, byte[] result)
        throws IOException {

    int bytesRead = 0;
    int total = result.length;
    int remaining = total;

    while (remaining > 0)
        remaining -= inputStream.read(result, bytesRead, remaining);

    return result;

}

The method used to convert a byte array to an integer:

private int byteArrayToInt(byte[] byteArray) {
    int result = 0;

    for (int i = 0; (i<byteArray.length) && (i<8); i++) {
        result |= (byteArray[3-i] & 0xff) << (i << 3);
    }
    return result;                  
}

The problem is, that the data is not read in the order it arrives. The first 4 bytes are being read just fine. The rest is mixed up. I made a TCP dump to ensure the data correctly arrives at the client. It seems as if the data is split up into 4 TCP packets. The InputStream returns the first 4 bytes of the first packet, then the entire data of the fourth packet, the last part (starting from "length of last packet") of the second packet and the entire data of the third packet. In this order.

Does anyone have a clue what might cause this issue?

1 Answer 1

2

Your logic for reading the byte array is not quite right:

From to the docs:

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

and

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

However, as your bytesRead variable stays at 0 for the whole loop, any data from the inputstream is always written to the beginning of your buffer, overwriting the data already in there.

What will work better (checking for -1 will also ensure that you don't subtract -1 from remaining if the stream runs out of data prematurely which would result in remaining increase, which would mean the loop would run unnecessarily until a buffer overrun would make remaining negative):

while ((bytesRead = inputStream.read(result, total - remaining, remaining)) != -1
     && remaining > 0) {
    remaining -= bytesRead;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. You just made my day (actually the whole next week, too) :)
Last snippet of code is very useful. It's easy to forget checking that return value. This is an easy 2 line solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.