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)?

gnat
  • 6,213
  • 108
  • 53
  • 73
Spoike
  • 119,724
  • 44
  • 140
  • 158

4 Answers4

5

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 Oct 14 '08 at 07:44
3

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(String)) so it might not be what you're looking for - but it does work across different phones/models etc.

Spoike
  • 119,724
  • 44
  • 140
  • 158
tonys
  • 3,855
  • 33
  • 39
3

Well... I know this was a LONG time ago.

You need to do exactly what John said, and it is VERY simple. It almost took me 5 hours to figure this one out the first time...

I still wonder why j2ME didn't include something as essential as the BufferedReader method for sockets, it's not like the freakin cellphones will crash with it... and yes, I don't give a rat's ass if my app runs 1ms slower than it should.

(I'm just going to put the relevant code, I assume you know how to form classes and import the required libraries)

ServerSocketConnection listener
    = (ServerSocketConnection)Connector.open("socket://:1235");
System.out.println("Waiting for connection...");
StreamConnection server = listener.acceptAndOpen();
InputStream is = server.openInputStream();

//Now comes the fake BufferedReader equivalent part

int ch = 0;
StringBuffer sb = new StringBuffer();

while ((ch = is.read()) != -1){
    sb.append((char)ch);
    if(sb.charAt(sb.length()-1) == 13 ) {
       //Carriage return was received or ENTER was pressed
       break; //Exit loop and print input
    }
}

As you can see, the is.read() method will lock the thread till new input is received from the user ONE BYTE AT A TIME. This means if you use telnet to test, each keystroke will make the loop iterate once, hence, we simply concatenate char by char in a StringBuffer until char 13 is received.

System.out.println(sb.toString());

I hope this helps people trying to do a socket server on j2ME. I already crafted a fully functional multithreaded version of this for blackberry, in case anyone needs it.

gnat
  • 6,213
  • 108
  • 53
  • 73
1

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143