Is it 100% safe (exception / error free) to convert a byte[] that includes random binary data to a String via the constructor:
new String(bytes);
// -- or --
new String(bytes,"UTF-8"); // Or other charset
My concern is whether invalid UTF-8 bytes will cause an exception or other failure instead of just a possibly partially garbled message.
I have tried some known bad byte values, as they appear to work as expected. E.g:
byte[] bytes = new byte[] {'a','b','c',(byte)0xfe,(byte)0xfe,(byte)0xff,(byte)0xff,'d','e','f'};
String test = new String(bytes,"UTF-8");
System.out.println(test);
Prints “abc????def”.
My concern is if certain other combinations can fail in other unexpected ways since I cannot guarantee that I can test every invalid combination.
This is covered in the docs:
One thing that will fail, if you’re not always using UTF-8, is that it can throw UnsupportedEncodingException.