...
This noncompliant code example reads a byte array and converts it into a String
using the platform's default character encoding. When the default encoding differs from the encoding that was used to produce the byte array, the resulting String
is likely to be incorrect. Undefined Unspecified behavior can occur when some of the input lacks a valid character representation in the default encodingresult from malformed-input and unmappable-character errors.
Code Block | ||
---|---|---|
| ||
FileInputStream fis = null; try { fis = new FileInputStream("SomeFile"); DataInputStream dis = new DataInputStream(fis); byte[] data = new byte[1024]; dis.readFully(data); String result = new String(data); } catch (IOException x) { // handle error } finally { if (fis != null) { try { fis.close(); } catch (IOException x) { // Forward to handler } } } |
...