Search code examples
javacharasciibytestringreader

Why doesn't StringReader.Read() return a byte?


I was using StringReader in a Data Structures assignment (Huffman codes), and was testing if the end of the string had been reached. I found that the int value that StringReader.read() returns is not -1, but 65535, so casting the result to a byte solved my infinite loop problem I was having.

Is this a bug in JDK, or is it common practice to cast values returned from Reader.read() calls to bytes? Or am I missing something?

The gist of my code was something like this:

StringReader sr = new StringReader("This is a test string");
char c;
do {
    c = sr.read();
//} while (c != -1);     //<--Broken
} while ((byte)c != -1); //<--Works

Solution

  • In fact that doesn't even compile. I get:

    Type mismatch: cannot convert from int to char

    Since the sr.read() call returns an int I suggest you store it as such.

    This compiles (and works as expected):

    StringReader sr = new StringReader("This is a test string");
    int i;               // <-- changed from char
    do {
        i = sr.read();
    
        // ... and if you need a char...
        char c = (char) i;
    
    } while (i != -1);   // <-- works :-)
    

    Why doesn't StringReader.Read() return a byte?

    Strings are composed of 16-bit unicode characters. These won't fit in an 8-bit byte. One could argue that a char would have been enough, but then there is no room for providing an indication that the EOF is reached.