load unicode text file, in J2ME

C

Chameleon

I have write this member function for reading a text file in UC-16BE
format (means unicode, 2 bytes/char, big endian)

----------------------------------------------------------
static public String loadFile(String s) throws Exception {
DataInputStream isr = new
DataInputStream(FileLoader.class.getResourceAsStream(s));
s = "";
for(int z = isr.available() / 2; z > 0; z--)
s += isr.readChar();
return s.substring(1);
}
----------------------------------------------------------

the problem is, that this function is slower than I want, because it
reads one by one characters from file.

Is there any better idea?

I don't care if I must convert my text files from UC-16BE to UC-16LE or
UTF-8, so feel free to make any changes (but not 1 byte/char).

Thanks a lot
 
C

Chris Uppal

Chameleon said:
static public String loadFile(String s) throws Exception {
DataInputStream isr = new
DataInputStream(FileLoader.class.getResourceAsStream(s));
s = "";
for(int z = isr.available() / 2; z > 0; z--)
s += isr.readChar();
return s.substring(1);
}

Don't use a DataInputStream -- there are only a few valid uses for
DataInputStream and this isn't one of them.

Use an InputStreamReader wrapped around a BufferedInputStream wrapped around
your input stream. With the buffering, the no-args version of the read()
method, which returns a single character each time, will be fast enough for
nearly all purposes. Set the charset for the InputStreamReader to something
like "UTF-16BE" in its constructor.

Never use the .available() method of any kind of stream -- it doesn't do what
you almost certainly think it does. That method is very misleading, and is
essentially useless. .

Don't build Strings by repeated uses of +. Use a java.lang.StringBuilder (or
StringBuffer if you are pre 1.5).

-- chris
 
D

Darryl L. Pierce

Chris said:
Use an InputStreamReader wrapped around a BufferedInputStream wrapped around
your input stream.

BufferedInputStream is not available in JavaME.
Don't build Strings by repeated uses of +. Use a java.lang.StringBuilder (or
StringBuffer if you are pre 1.5).

StringBuilder is not available in JavaME.
 
D

Darryl L. Pierce

Chameleon said:
I have write this member function for reading a text file in UC-16BE
format (means unicode, 2 bytes/char, big endian)

----------------------------------------------------------
static public String loadFile(String s) throws Exception {
DataInputStream isr = new
DataInputStream(FileLoader.class.getResourceAsStream(s));
s = "";
for(int z = isr.available() / 2; z > 0; z--)
s += isr.readChar();
return s.substring(1);
}
----------------------------------------------------------

The slowness is the result of going out to where the data is stored in
pieces. What you should do instead is to load the content of your file
into a ByteArrayInputStream and then process it from there:

public String loadFile(String s) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];

InputStream istream = myMidlet.getSourceAsStream(s);
boolean done = false;

while(!done) {
int count = istream.ready(buffer);
baos.write(buffer,0,count);
}

byte[] content = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(content);
DataInputStream isr = new DataInputStream(bais);

// now do your processing
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top