Created
August 26, 2019 11:08
-
-
Save VenkataRaju/72215895d3495f92945f27acef75472c to your computer and use it in GitHub Desktop.
Java Unicode CodePoint Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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