views:

1896

answers:

3

I'm using the MIDP 2.0 (JSR 118) and I just noticed that there is no reader for strings in J2ME.

Does anyone know how you are supposed to read Strings from an InputStream or InputStreamReader in a platform independent way (i.e. between two java enabled cell phones of different models)?

+2  A: 

Which profile are you using? The MID profile in JSR 118 specifies InputStreamReader (not StringReader, but that wouldn't help you read from an InputStream anyway).

EDIT: To reflect the change to the question :)

You use InputStreamReader.read(char[], int, int) and when you've read all you want to, create a new string from a char array. If you want to read a line at a time as you would from BufferedReader, you basically need to implement the functionality of BufferedReader yourself (keeping a buffer of "read but not consumed" chars) and keep reading until you hit a line break.

Jon Skeet
Yeah, I'm using MIDP 2.0 (JSR 118). Edited my question. I want to know if there is some way to read strings from any given InputStream.
Spoike
+1  A: 

Alternatively have a look at DataInputStream.readUTF().

It does required that the string being read off the InputStream be encoded appropriately (as in by a corresponding DataOutputStream.writeUTF) so it might not be what you're looking for - but it does work across different phones/models etc.

tonys
A: 

Would you be able to provide an example of this?

You use InputStreamReader.read(char[], int, int) and when you've read all you want to, create a new string from a char array. If you want to read a line at a time as you would from BufferedReader, you basically need to implement the functionality of BufferedReader yourself (keeping a buffer of "read but not consumed" chars) and keep reading until you hit a line break.

Thanks