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
Chris Uppal - 07 Aug 2006 09:27 GMT
> static public String loadFile(String s) throws Exception {
> DataInputStream isr = new
[quoted text clipped - 4 lines]
> 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
Darryl L. Pierce - 07 Aug 2006 12:04 GMT
> 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.

Signature
Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard
--
Posted via a free Usenet account from http://www.teranews.com
Darryl L. Pierce - 07 Aug 2006 12:07 GMT
> I have write this member function for reading a text file in UC-16BE
> format (means unicode, 2 bytes/char, big endian)
[quoted text clipped - 9 lines]
> }
> ----------------------------------------------------------
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
}

Signature
Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard
--
Posted via a free Usenet account from http://www.teranews.com