javastringstringreader

java.io.StringReader.read() (Java 8) returns unexpected character at the end of the String


this problem causes an infinite loop in the following code excerpt:

    public static final List<String> extractTags(String source, Integer nTags) {

    List<String> tags = new ArrayList<>();

    try (StringReader stringReader = new StringReader(source)) {
      String tag = "";
      char c;
      while ((c = (char) stringReader.read()) >= 0 && tags.size() < nTags) {
        switch (c) {
        case '<':
          tag = "";
          break;
        case '>':
          tags.add(tag);
          break;
        default:
          tag = tag + c;
          break;
        }
      }
    } catch (IOException e) {
    } finally {
      return tags;
    }
  }

if invoked with the following parameters: source = "trash" nTags = 2

Using a debugger I realized that after the string was completely iterated, the read() method returns the char '\uFFFF' 65535 forever. So my question is why?

Thanks!


Solution

  • Because stringReader.read() is returning -1 for end of stream, but you're casting it to char which is the only unsigned datatype in Java. So instead of -1 you're getting 65535 for end of stream therefore never breaking the while loop.

    You might want to cast what is read into a char inside the loop, instead of at the while condition.