Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: box fix

...

This noncompliant code example corrupts the data when string contains characters that are not representable in the specified charset.

 

Code Block
bgColor#FFcccc
// Corrupts data on errors
public static byte[] toCodePage(String charset, String string)
  throws UnsupportedEncodingException {
  return string.getBytes(charset);
}
 
// Fails to detect corrupt data
public static String fromCodePage(String charset, byte[] bytes)
  throws UnsupportedEncodingException {
  return new String(bytes, charset);
}
 

Compliant Solution

The java.nio.charset.CharsetEncoder class can transform a sequence of 16-bit Unicode characters into a sequence of bytes in a specific charset, while the java.nio.charset.CharacterDecoder class can reverse the procedure [API 2006].

...