1

嗨,这是我在这里的第一个问题,所以如果由于某种原因不遵守规则,结果是重复的或其他什么,请告诉我(不是我一开始就失去任何声誉)

无论如何,我实际上对 Java 提供的此类 StringReader 有 2 个问题。首先,StringReader.ready() 到底是做什么的?我可以将它用作 while 循环中的条件,以便在字符串结束时终止循环吗?阅读 java 文档并没有太大帮助(或者我可能误解了他们所说的“如果保证下一个 read() 不会阻塞输入,则返回 True”的意思)

更新:

很抱歉,我错过了read()字符串结束时返回 -1 的部分。无论如何,我的问题仍然是 ready() 部分。我认为这应该是检查字符串是否结束的那个?

任何帮助将不胜感激,谢谢!

链接到实际的源代码

导致问题的片段:

while (text.ready()) {

        // Updating stringBuffer, using some sort of 'rolling hash' (or is
        // it
        // indeed rolling hash?)
        stringBuffer.deleteCharAt(0);
        stringBuffer.append((char) next);

        // The next character that follows the sequence of k characters
        next = text.read();

        // store the string form of the buffer to avoid rebuilding the
        // string for the next few checks
        String key = stringBuffer.toString();

        if (hashmap.containsKey(key)) {
            // If the hash map already contain the key, retrieve the array
            asciiArray = hashmap.get(key);
        } else {
            // Else, create a new one
            asciiArray = new int[128];
        }
        System.out.println(next);

        // Error checking on my side only, because some of the text sample I
        // used contains some characters that is outside the 128 ASCII
        // character, for whatever reason
        if (next > 127) {
            continue;
        }

        // Increment the appropriate character in the array
        asciiArray[next]++;
        // Put into the hash map
        hashmap.put(key, asciiArray);

    }
4

3 回答 3

3

首先,StringReader.ready() 到底是做什么的?

一般的约定是,如果下一次读取不会阻塞,则返回 true。在这种情况下StringReader总是如此。

我可以将它用作 while 循环中的条件,以便在字符串结束时终止循环吗?阅读 java 文档并没有太大帮助(或者我可能误解了他们所说的“如果保证下一个 read() 不会阻塞输入,则返回 True”的意思)

不,一个简单的测试就可以证明这一点。您应该循环直到read()返回-1。请注意,您必须将结果存储read()到 anint中才能正常工作。

在我构建的 while 循环中,方法 StringReader.read() 以某种方式返回 -1。

没有“不知何故”。这就是它应该做的。

这是什么意思?

这意味着流的结束。在这种情况下,您已从StringReader.

同样,Java 文档也没有帮助。

相反。Javadoc明确指出返回“读取的read()字符,如果已到达流的末尾,则返回 -1”。

我猜这意味着字符串已经终止

无需猜测。这就是Javadoc 所说的。

但这意味着 ready() 方法没有做它应该做的事情!

不,它没有。Javadoc 没有说在流结束时ready()返回。false你对那个声明没有任何保证。在这种情况下,它返回 true,您调用read()了 ,并且它没有阻塞。合同满意。

于 2014-05-06T04:06:05.503 回答
0

为了帮助您更好地理解这个概念,假设您正在以每秒一个字符的速度从一个slooooow TCP 连接StringReader中读取一系列字符......

如果您随后StringReader.read()在 while 循环中调用,则每次read()将等待 1 秒钟,然后再返回一个字符。如果您不希望代码挂在那里等待字符进入,这可能是一个问题。这时候StringReader.ready()就变得很方便了。如果它返回 false,那么这意味着调用read()可能会导致代码阻塞(等待),而如果ready()返回 true,则意味着调用read()立即返回一个值(字符或 -1)而不会阻塞/等待。

现在假设所有字符已经通过连接传输,因此连接被关闭,那么ready()仍然会返回 true,因为它知道它不需要等待下一个字符(因为没有)。现在,当您调用 时read(),它也会立即返回,但这次是 -1,表示没有更多字符。

现在让我们回到您的代码。对于您想要实现的目标,您不需要使用ready(). 相反,您的代码的第一行应更改为:

int ch;

while (true)
{
    ch = text.read();
    if (ch < 0) break;

    ...
于 2014-05-06T04:12:51.540 回答
0

如果您查看StringReader.ready()方法的源代码,您可以找到答案。StringReader是一个装饰器类,用于String使用阅读器读取 s。您只能StringReader通过传入 String 参数来创建实例。

StringReader.ready()始终返回 true,并且仅在实例化IOExceptionString传递的值为 null时才抛出

 public boolean ready() throws IOException {
     synchronized (lock) {
        ensureOpen();
        return true;
     }
 }

 /** Check to make sure that the stream has not been closed */
 private void ensureOpen() throws IOException {
     if (str == null)
        throw new IOException("Stream closed");
 }
于 2014-05-06T04:13:10.943 回答