×
Namespaces

Variants
Actions

Archived:Reading resources from a JAR file on S60 2nd Edition (Known Issue)

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): Nokia 6600
CompatibilityArticle
Created: User:Technical writer 1 (20 Aug 2007)
Last edited: hamishwillee (13 Aug 2013)

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);
This page was last modified on 13 August 2013, at 14:12.
50 page views in the last 30 days.
×