Symbian
Symbian OS Library

FAQ-0964 My MIDlet fails to read from an InputStream. Whats wrong?

[Index][spacer] [Previous] [Next]



 

Classification: Java Category: J2ME MIDP 2.0
Created: 01/19/2004 Modified: 01/27/2004
Number: FAQ-0964
Platform: Symbian OS v7.0s

Question:
My MIDlet fails to read from an InputStream when running on the Nokia 6600/SonyEricsson P900. Whats wrong?

Answer:
MIDP 2.0 supporting devices such as the Nokia 6600 and Sony Ericcson P900 make use of Sun's high performance CLDC HI 1.0 VM (also known as Monty).
    This highly optimised VM strictly interprets the Java Specification relating to reading from InputStreams. The following is an extract from the Java documentation for two of the read methods of the InputStream class.

    public int read(byte[] data) throws IOException
    "Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer."

    public int read(byte[] data, int offset, int len) throws IOException
    "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, possibly zero. The number of bytes actually read is returned as an integer. "

    In other words the specification doesn't guarantee that a single invocation of read(...) will read the entire contents of the input stream. A consequence of this is that the following may fail on the Monty VM. try{
    InputStream in = getClass().getResourceAsStream(resource);
    DataInputStream din = new DataInputStream(in);
    byte[] data = new byte[IMAGE_SIZE];
    din.read(data);
    din.close();
    Image image = Image.createImage(data, 0, data.length);
    ...
    }catch(IOException ioe) {...}

      A simple workaround for the above example is to use the

      public final void readFully(byte[] b) throws IOException

      method of DataInputStream instead.


      Futher information can be found in the following document:

      "Known Issues in the Nokia 6600 MIDP 2.0 Implementation Version 1.0 " (www.forum.nokia.com)