Last active
November 1, 2016 06:19
-
-
Save dmikurube/a3659e09ab9b2dd48571d3a7d2f53627 to your computer and use it in GitHub Desktop.
UTF-8 Conversion in JDK 1.7 and 1.8
This file contains hidden or 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
import java.nio.ByteBuffer; | |
import java.nio.charset.CharacterCodingException; | |
import java.nio.charset.Charset; | |
import java.nio.charset.CharsetDecoder; | |
import java.nio.charset.CodingErrorAction; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class UTF8PerJavaVersion { | |
public static void main(String[] args) throws CharacterCodingException { | |
byte[] b_org = new byte[] { (byte) 0xfd, (byte) 0xfc }; | |
for (byte b_each : b_org) { | |
System.out.printf(" %02X", b_each); | |
} | |
System.out.println(""); | |
String s = convertByteArrayToString(b_org); | |
System.out.println(s); | |
for (char c : s.toCharArray()) { | |
System.out.printf(" %04X", (int)c); | |
} | |
System.out.println(""); | |
byte[] b_new = s.getBytes(charset); | |
for (byte b_each : b_new) { | |
System.out.printf(" %02X", b_each); | |
} | |
System.out.println(""); | |
} | |
private static String convertByteArrayToString(byte[] b) throws CharacterCodingException { | |
CharsetDecoder decoder = Charset.forName("UTF-8") | |
.newDecoder() | |
.onMalformedInput(CodingErrorAction.REPLACE) | |
.onUnmappableCharacter(CodingErrorAction.REPLACE); | |
return decoder.decode(ByteBuffer.wrap(b)).toString(); | |
} | |
private static final Charset charset = Charset.forName("UTF-8"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug: https://bugs.openjdk.java.net/browse/JDK-7082883
Bug: https://bugs.openjdk.java.net/browse/JDK-7082884
Summary: https://bugs.openjdk.java.net/browse/JDK-7096080
Change: http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/417d91754849
FYI: http://answerforqa.weblog.to/archives/1038977704.html
FYI: http://stackoverflow.com/questions/25404373/java-8-utf-8-encoding-issue-java-bug/25404994#25404994