This page was last modified 10:12, 23 August 2007.
KIJ000081 - Reading resources from a JAR file
From Forum Nokia Wiki
Platform: S60 2nd Edition Device: Nokia 6600
Description:
The MIDP 2.0/Monty implementation for the InputStream.read(byte[]) method in the Nokia 6600 device takes a literal reading of the definitions of java.io.InputStream.read(bytes[] b) and java.io.InputStream.read(byte[] b, int off, int len), which indicate that these functions might NOT read the number of bytes equal to the size of the array argument (b.length) before returning, and will indicate the number of bytes that were successfully read as a return value. This is basically because it is a more optimized approach for the implementation.
NOTE: Because all other InputStream classes (for example, DataInputStream and ByteArrayInputStream) are subclasses of InputStream, this issue is the same for ALL InputStream operations.
Solution:
The Java™ programmer (in this case for MIDlets on the Nokia 6600 device, but this is generally the best practice) should always loop until reading is complete and not assume a resource will be read in one go; see the following examples of wrong and right code:
WRONG
InputStream in = getClass().getResourceAsStream(resource);
if (in == NULL) return;
DataInputStream din = new DataInputStream(in);
int bytes = din.read(data);
din.close();
Image I = Image.createImage(data, 0, offset);
RIGHT
InputStream in = getClass().getResourceAsStream(resource);
if (in == null) return;
DataInputStream din = new DataInputStream(in);
int bytes = 0;
int offset = 0;
while (true) {
bytes = din.read(data, offset, data.length - offset);
offset += bytes;
if (bytes == -1
| offset >= data.length) {
// may need to handle error condition here!
break;
}
}
din.close();
// now it’s safe to assume data is already fully read (or error
// occurred) for example, if it was image data..
Image I = Image.createImage(data, 0, offset);
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Carbide resources file? | mayankkedia | Carbide.c++ and CodeWarrior Tools | 7 | 2007-02-09 10:57 |
| Can .JAD Files/Settings be deployed from PC? (Via bluetooth) | Niall | Mobile Java Tools & SDKs | 0 | 2002-11-09 01:07 |
| Reading ints from a file | xdimas | Mobile Java General | 10 | 2003-07-21 10:01 |
| Localication & unicode | mcmcdonald | Mobile Java General | 12 | 2006-01-11 07:22 |
| Porting from S60v2 to S60v3 | vishnu_iiit | Porting Symbian C++ to S60 | 9 | 2007-03-18 05:34 |

