Reading an inputStream all at once [duplicate]
Asked Answered
A

3

22

I have developed a j2me application that connects to my webhosting server through sockets. I read responses from the server using my own extended lineReader class that extends the basic InputStreamReader. If the server sends 5 lines of replies, the syntax to read the server replies line by line is:

        line=input.readLine();
        line = line + "\n" + input.readLine();
        line = line + "\n" + input.readLine();
        line = line + "\n" + input.readLine();
        line = line + "\n" + input.readLine();

In this case, i can write this syntax because i know that there is a fixed number of replies. But if I dont know the number of lines, and want to read the whole inputStream at once, how should I modify the current readLine() function. Here's the code for the function:

public String readLine() throws IOException {
    StringBuffer sb = new StringBuffer();
    int c;
    while ((c = read()) > 0 && c != '\n' && c != '\r' && c != -1) {
        sb.append((char)c);
    }
    //By now, buf is empty.
    if (c == '\r') {
        //Dos, or Mac line ending?
        c = super.read();
        if (c != '\n' && c != -1) {
            //Push it back into the 'buffer'
            buf = (char) c;
            readAhead = true;
        }
    }
    return sb.toString();
}
Aussie answered 22/4, 2011 at 1:36 Comment(0)
G
10

What about Apache Commons IOUtils.readLines()?

Get the contents of an InputStream as a list of Strings, one entry per line, using the default character encoding of the platform.

Or if you just want a single string use IOUtiles.toString().

Get the contents of an InputStream as a String using the default character encoding of the platform.

[update] Per the comment about this being avaible on J2ME, I admit I missed that condition however, the IOUtils source is pretty light on dependencies, so perhaps the code could be used directly.

Garton answered 22/4, 2011 at 1:41 Comment(3)
but is it available for the j2me platform?Aussie
@Coding-Freak: sorry I missed that; I am somewhat ignorant about the J2ME platform so I updated my answer with the best I know of, good luck.Garton
I just want to modify my currently working readLine() function to be able to read the whole stream all at once. Hope you understand the question.Aussie
P
1

If I understand you correctly, You can use a simple loop:

StringBuffer sb = new StringBuffer();
String s;
while ((s = input.readLine()) != null)
    sb.append(s);

Add a counter in your loop, and if your counter = 0, return null:

int counter = 0;
while ((c = read()) > 0 && c != '\n' && c != '\r' && c != -1) {
    sb.append((char)c);
    counter++;
}
if (counter == 0)
    return null;
Pinelli answered 22/4, 2011 at 2:6 Comment(4)
i modified the code as you said. now its returning no response from the server.Aussie
This is not an entire example. you should add some EOF flag if -1 has received and not read after that.Pinelli
please help me with the rest code. i am totally stuck with it. :(Aussie
I can't write a real and tested code right now anyhow (since I don't have java environment on this computer) but why don't you use the while loop I suggested (at the beginning of my answer) with the original readLine, not your extension?Pinelli
B
1

Specifically for web server !

String temp;
StringBuffer sb = new StringBuffer();
while (!(temp = input.readLine()).equals("")){
    sb.append(line);
}
Burglar answered 26/1, 2013 at 23:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.