Search code examples
javajava-memidp

HttpConnection truncating messages in Sony Ericsson W580


I've been having a problem while using HttpConnection with a Sony Ericsson W580.

The response to my http requests is application/octet-stream, and I'm sending a quite large array of bytes.

In this mobile phone however, it is consistently being cut down to 210 bytes...

I've tested the MIDP application in a large number of different mobile phones, using different mobile carriers and wi-fi, and no other mobile has shown this behavior.


Solution

  • Ok, I found the problem. Entirely my fault...

    How I was reading the stream:

    while(true){
        int bytesRead = stream.read(tmpBuffer);
        // if -1, EOF
        if(bytesRead < 0)
            break;
    
        (...)
    
        // WRONG LOGIC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // if we read the last chunk of data, and found EOF
        if(bytesRead < tmpBufferArrayLength)
            break;
        // WRONG LOGIC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
    

    See the part between the "wrong logic !!!" comments? I was assuming that if read(tmpBuffer) could not fill the tmp buffer entirely, it was because EOF was being reached. The API does not guarantee this at all: it just states that EOF is signaled by a read(tmpBuffer) returning -1.

    I didn't see this before because all the mobiles (and emulatores) I'd tested were being able to fill completely the buffer at every call.