Post

Detecting Big5 Encoding Errors in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static final Charset BIG5 = Charset.forName("Big5");
private static final CharsetEncoder ENC = BIG5.newEncoder()
		.onUnmappableCharacter(CodingErrorAction.REPORT)
		.onMalformedInput(CodingErrorAction.REPORT);

public static int firstUnencodableIndex(String s) {
	CharBuffer in = CharBuffer.wrap(s);
	ByteBuffer out = ByteBuffer.allocate((int)(s.length() * ENC.maxBytesPerChar()));
	ENC.reset();
	int i = 0;
	while (in.hasRemaining()) {
		char c = in.get();
		CoderResult r = ENC.encode(CharBuffer.wrap(new char[]{c}), out, false);
		if (r.isUnmappable()) return i; // the first character could not be encoded
		if (r.isError()) return i;
		i++;
	}
	return -1; // All are fine.
}
This post is licensed under CC BY 4.0 by the author.