一次读取输入流

发布于 2024-11-02 23:10:45 字数 1037 浏览 1 评论 0原文

我开发了一个 j2me 应用程序,通过套接字连接到我的虚拟主机服务器。我使用自己的扩展 lineReader 类读取来自服务器的响应,该类扩展了基本的 InputStreamReader。如果服务器发送 5 行回复,则逐行读取服务器回复的语法为:

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

在这种情况下,我可以编写此语法,因为我知道回复的数量是固定的。但是如果我不知道行数,并且想一次读取整个 inputStream,我应该如何修改当前的 readLine() 函数。这是该函数的代码:

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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强者自强 2024-11-09 23:10:45

Apache Commons 怎么样 IOUtils.readLines()

使用平台的默认字符编码,以字符串列表的形式获取 InputStream 的内容,每行一个条目。

或者,如果您只想要单个字符串,请使用 IOUtiles.toString()

使用平台的默认字符编码以字符串形式获取 InputStream 的内容。

[更新]根据关于 J2ME 上可用的评论,我承认我错过了这个条件,但是 IOUtils 源代码 的依赖关系很小,因此也许可以直接使用代码。

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.

凉世弥音 2024-11-09 23:10:45

如果我理解正确的话,您可以使用一个简单的循环:

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

在循环中添加一个计数器,如果您的计数器 = 0,则返回 null:

int counter = 0;
while ((c = read()) > 0 && c != '\n' && c != '\r' && c != -1) {
    sb.append((char)c);
    counter++;
}
if (counter == 0)
    return null;

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;
岁月苍老的讽刺 2024-11-09 23:10:45

专门用于网络服务器!

String temp;
StringBuffer sb = new StringBuffer();
while (!(temp = input.readLine()).equals("")){
    sb.append(line);
}

Specifically for web server !

String temp;
StringBuffer sb = new StringBuffer();
while (!(temp = input.readLine()).equals("")){
    sb.append(line);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文