Hello Alexis,
I found the SNI.toJavaString method takes bytes as they are, regardless of their encoding, and make a string with one char = one byte. So, at the end, when the websocket sendText uses getBytes() (with default platform encoding), the original bytes are restored in the returned array.
Also, when the websocket receives an UTF-8 encoded text message from the server, the bytes are taken one by one to make a string.
So, the string are stored in a special way in String object, but in that way, there is no encoding issues.
The only case it can be a issue is when the string is made with a litteral text.
...
String test2 = new String("modèle"); // Incorrect
String test3 = toJavaString("modèle"); // Correct
// 'm', 'o', 'd' 'è' 'l' 'e'
bytes[] btest4 = { 0x6d, 0x6f, 0x64, 0xc3, 0xa8, 0x6c, 0x65 };
String test4 = toJavaString(btest4);
...
/* The following function is similar to SNI.toJavaString(byte[] cString), but it doesn't require a NULL byte termination*/
String toJavaString(byte[] b) {
return new String(b);
}
String toJavaString(String s) {
byte[] b = s.getBytes("UTF-8");
return toJavaString(b);
}
if we call the getBytes() on test2, we will not get an UTF-8 array : { 0x6d, 0x6f, 0x64,
0xe8
, 0x6c, 0x65 }
If we call the getBytes() on test3, we will get an UTF-8 array : { 0x6d, 0x6f, 0x64,
0xc3, 0xa8
, 0x6c, 0x65 }
I just wanted to make things more clear. I would like you to avoid just putting the “UTF-8” argument in the getBytes() method when forming the message to send to the websocket.
Best regards
Benoit