creating an image from recordstore ...

i have the following task:
an image file gets read via http-connection from a server. this byte-array is written to a recordstore and loaded for creating (restoring) the image.

theoretically this works fine. i got real devices and emulators which perform this without any issues but others seem to have quite big problems creating the image from recordstore.
i am very sure that the bytes get read from server and written into recordstore properly (number of bytes is correct, i can output the png-header etc).

first, the code to read and create the image:


public static Image getImage(String recordStoreName) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException, IOException
    {
        RecordStore store = RecordStore.openRecordStore(recordStoreName, false);
        // input streams
        ByteArrayInputStream bais = new ByteArrayInputStream(store.getRecord(1));
        DataInputStream in               = new DataInputStream(bais);
        
        // reading length of data
        int imageDataLength = in.readInt();

        byte[] rawData = new byte[imageDataLength];
        in.read(rawData, 0, rawData.length);
        
        store.closeRecordStore();
        bais.close();
         
        return Image.createImage(rawData, 0, imageDataLength);
    }

the only modification ill do is to write an integer value at the beginning of the recordstore to measure the length of bytes to come.

the results are very different on phones:
sharp gx10 does it with no problems, others just hang or seem to have problems with creating the image from png data


Image.createImage(rawData, 0, imageDataLength);

there ill get BadArgumentExceptions (though im sure the png data is correct), EofExceptions (huh?) or the image is just not displayable.

any hints, comments, tips ?!

Are you sure that:


in.read(rawData, 0, rawData.length); 

is actually reading in all the data?
Try checking the return value of that method and see if it’s actually the size of the data.

shmoove

[edit]
I would also try and make sure that the byte array contains the correct data by packagind the same image in the jar, opening it with getResourceAsStream and comparing it the the rawData array.
[/edit]

oohoo, i found the mistake. i am the error. :-[
today i learned the rule again: RTFM!

the problem was indeed that the data wasnt load properly from the server. i used an inputstream with the read(byte[] )
method. but i only flew over the api and did not read the passage

now i use a DataInputStream.readFulle(…) and all problems are gone … shame on me…

I seem to remember readFully(…) has its own set of quirks on certain handsets, as does available();

As with all mobile development, its trial and error =/

hmm, dont make make another headache now. ive tested it succsessfully on all available phones here and thought the problem was solved.
but nothing i can do about it now, as with all mobile development … :wink:

I ran into the same problem when I swapped from the emulator to certain handsets. If readFully() is giving you trouble, you might want to try something like this:


lengthSoFar = 0;                        
do
{
    readLength = iStrm.read(buffer, lengthSoFar, expectedLength-lengthSoFar);
    lengthSoFar += readLength;
} while (lengthSoFar < expectedLength);