Skip to content

Instantly share code, notes, and snippets.

@zaki50
Last active December 27, 2015 17:59
Show Gist options
  • Save zaki50/7366567 to your computer and use it in GitHub Desktop.
Save zaki50/7366567 to your computer and use it in GitHub Desktop.
Kitkat での実行結果がこんなでつらい
D/hoge ( 8203): "あ".getBytes(Shift_JIS).length = 1
D/hoge ( 8203): "あい".getBytes(Shift_JIS).length = 3
D/hoge ( 8203): "あいう".getBytes(Shift_JIS).length = 6
D/hoge ( 8203): "あいうえ".getBytes(Shift_JIS).length = 8
D/hoge ( 8203): "あいうえお".getBytes(Shift_JIS).length = 10
private void test() {
try {
final Charset charset = Charset.forName("Shift-JIS");
logByteLength("あ", charset);
logByteLength("あい", charset);
logByteLength("あいう", charset);
logByteLength("あいうえ", charset);
logByteLength("あいうえお", charset);
} catch (UnsupportedEncodingException e) {
Log.d("hoge", "", e);
}
}
private void logByteLength(String str, Charset charset) {
final byte[] bytes = str.getBytes(charset);
Log.d("hoge", "\"" + str + "\".getBytes(" + charset.displayName() + ").length = " + bytes.length);
}
gistfile3.txt のコードにするとこんなかんじ
D/hoge (10513): byte count of Charset.forName("Shift_JIS").newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).encode("あ") = 1
D/hoge (10513): byte count of Charset.forName("Shift_JIS").newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).encode("あい") = 3
D/hoge (10513): byte count of Charset.forName("Shift_JIS").newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).encode("あいう") = 6
D/hoge (10513): byte count of Charset.forName("Shift_JIS").newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).encode("あいうえ") = 8
D/hoge (10513): byte count of Charset.forName("Shift_JIS").newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).encode("あいうえお") = 10
private void logByteLength(String str, Charset charset) {
try {
final ByteBuffer buffer = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE).encode(
CharBuffer.wrap(str));
Log.d("hoge", "byte count of Charset.forName(\"" + charset.displayName() + "\").newEncoder()" +
".onMalformedInput(CodingErrorAction.REPLACE)" +
".onUnmappableCharacter(CodingErrorAction.REPLACE)" +
".encode(\"" + str + "\") = " + buffer.remaining());
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment