Performing conversions of Converting String
objects between to different character encodings or to byte arrays may result in loss of data.
...
This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array.
When a String
must be object is converted to bytes, for example, for writing to a file, and the string might contain sequences of unmappable characters, proper character encoding must be performed.
Converting a byte array to a string has similar consequences issues if the byte array does is not indicate a valid encoded string. Attempts to read raw binary data as if it were character-encoded data often fail because some of the will not succeed if bytes fall outside the default or specified encoding scheme and for that reason consequently fail to denote valid characters. For example, converting a cryptographic key containing nonrepresentable bytes to character-encoded data for transmission may result in an error.
...