Search code examples
javastringreader

Java StringReader ready()


Hi this is my first question here so if it is for some reason does not obey the rules, turns out to be a duplicate or whatever, please tell me kindly (not that I have any reputation to lose in the first place)

Anyway, I actually have sort of 2 questions about this class StringReader that Java provides. First, what exactly the StringReader.ready() do? Can I use it as a condition in a while loop so that the loop terminates when the string ends? Reading the java doc didn't help much (or maybe I misunderstood what they meant by "Returns True if the next read() is guaranteed not to block for input")

Update:

Sorry apparently I missed the part where it says read() returns -1 when the string ends. Anyway, my question remains for the ready() part. I thought this is supposed to be the one that checks if the string has ended?

Any help would be appreciated, thanks!

Link to the actual source code

The snippet that causes the problem:

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

    }

Solution

  • First, what exactly the StringReader.ready() do?

    The general contract is that returns true if the next read won't block. In the case of StringReader that is always true.

    Can I use it as a condition in a while loop so that the loop terminates when the string ends? Reading the java doc didn't help much (or maybe I misunderstood what they meant by "Returns True if the next read() is guaranteed not to block for input")

    No. A simple test reveals that. You should loop until read() returns -1. Note that you must store the result of read() into an int for this to work.

    In the while loop that I have constructed, the method StringReader.read() somehow returns -1.

    No 'somehow' about it. That's what it's supposed to do.

    What does this mean?

    It means end of stream. In this case, you've read all the characters out of the StringReader.

    Again, the Java doc did not help.

    On the contrary. The Javadoc clearly states that read() returns "the character read, or -1 if the end of the stream has been reached".

    I am guessing that it means that the string has already terminated

    No need to guess about it. That's explicitly what the Javadoc says.

    but then that means the ready() method is not doing what it is supposed to do!

    No it doesn't. The Javadoc doesn't say that ready() returns false at end of stream. You don't have any warrant for that statement. In this case, it returned true, you called read(), and it didn't block. Contract satisfied.