...
This noncompliant code example attempts to convert the byte array representing a BigInteger
into a String
. Because some of the bytes do not denote valid characters, the resulting String
representation loses information. Converting the String
back to a BigInteger
produces a different value.
Code Block | ||||
---|---|---|---|---|
| ||||
BigInteger x = new BigInteger("530500452766"); // convert x to a String byte[] byteArray = x.toByteArray(); String s = new String(byteArray); // convert s back to a BigInteger byteArray = s.getBytes(); x = new BigInteger(byteArray); |
...
This compliant solution first produces a String
representation of the BigInteger
object and then converts the String
object to a byte array. This process is reversed on input. Because the textual representation in the String
object was generated by the BigInteger
class, it contains valid characters.
Code Block | ||||
---|---|---|---|---|
| ||||
BigInteger x = new BigInteger("530500452766"); String s = x.toString(); // valid character data try { byte[] byteArray = s.getBytes("UTF8"); // ns prints as "530500452766" String ns = new String(byteArray, "UTF8"); // construct the original BigInteger BigInteger x1 = new BigInteger(ns); } catch (UnsupportedEncodingException ex) { // handle error } |
...
This noncompliant code example corrupts the data when string
contains characters that are not representable in the specified charset
.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// 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); } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
public static byte[] toCodePage(String charset, String string) throws IOException { Charset cs = Charset.forName(charset); CharsetEncoder coder = cs.newEncoder(); ByteBuffer bytebuf = coder.encode(CharBuffer.wrap(string)); byte[] bytes = new byte[bytebuf.limit()]; bytebuf.get(bytes); return bytes; } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
// Corrupts data on errors public static void toFile(String charset, String filename, String string) throws IOException { FileOutputStream stream = new FileOutputStream(filename, true); OutputStreamWriter writer = new OutputStreamWriter(stream, charset); writer.write(string, 0, string.length()); writer.close(); } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public static void toFile(String filename, String string, String charset) throws IOException { Charset cs = Charset.forName(charset); CharsetEncoder coder = cs.newEncoder(); FileOutputStream stream = new FileOutputStream(filename, true); OutputStreamWriter writer = new OutputStreamWriter(stream, coder); writer.write(string, 0, string.length()); writer.close(); } |
Use the FileInputStream
and InputStreamReader
objects to read back the data from the file. InputStreamReader
accepts a optional CharsetDecoder
argument, which must be the same as that previously used for writing to the file.
...