I'm having trouble reading in supplementary unicode characters using Java. I have a file that potentially contains characters in the supplementary set (anything greater than \uFFFF). When I setup my InputStreamReader to read the file using UTF-8 I would expect the read() method to return a single character for each supplementary character, instead it seems to split on the 16 bit threshold.
I saw some other questions about basic unicode character streams, but nothing seems to deal with the greater than 16 bit case.
Here's some simplified sample code:
InputStreamReader input = new InputStreamReader(file, "UTF8");
int nextChar = input.read();
while(nextChar != -1) {
...
nextChar = input.read();
}
Does anyone know what I need to do to correctly read in a UTF-8 encoded file that contains supplementary characters?
Java works with UTF-16. So, if your input stream has astral characters, they will appear as a surrogate pair, i.e., as two char
s. The first character is the high surrogate, and the second character is the low surrogate.