Last active
December 27, 2015 17:59
-
-
Save zaki50/7366567 to your computer and use it in GitHub Desktop.
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
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 |
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
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); | |
} |
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
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 |
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
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