Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
Created August 26, 2019 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VenkataRaju/72215895d3495f92945f27acef75472c to your computer and use it in GitHub Desktop.
Save VenkataRaju/72215895d3495f92945f27acef75472c to your computer and use it in GitHub Desktop.
Java Unicode CodePoint Reader
public final class CodePointReader implements AutoCloseable
{
private final Reader reader;
public CodePointReader(Reader reader)
{
this.reader = reader;
}
public int read()
{
try
{
int highInt = reader.read();
if (highInt == -1)
return -1;
char highChar = (char) highInt;
if (!Character.isHighSurrogate(highChar))
return highInt;
int lowInt = reader.read();
char lowChar = (char) lowInt;
if ((lowInt == -1) || !Character.isLowSurrogate(lowChar))
throw new RuntimeException("No corresponding low surrogate found [found '" + lowChar + "] for high surrogate [" + highChar + "]");
return Character.toCodePoint(highChar, lowChar);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public void close() throws Exception
{
reader.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment