Joeblackdev
Joeblackdev

Reputation: 7327

different jvms with different encodings

Suppose I have 2 jvms running - 1 is a client and the other is a server. Suppose the client and server are using different encodings. If I write a program on the client which sends Strings across the network to the server, is it necessary to encode the String in the client in the server's encoding before the client sends it across to the server? Would this be pointless if the 2 are using different encodings in the first place? How do clients and servers handle scenarios typically where they are exchanging messages where both are using different encodings?

Upvotes: 1

Views: 79

Answers (2)

anonymous
anonymous

Reputation: 1317

I suppose you are encountering what is called platform default encoding. For example, when converting bytes into String using new String(byte[]), the default encoding is used to convert bytes to String. Different servers may have different setup that have a different default platform encoding.

To prevent different behaviour of the servers due to different default encoding, specify the encoding to use when converting bytes[] to String. If you don't know the encoding to use, that is another matter but at least you get consistent results for the same byte stream.

For example, to convert String to UTF-8 byte stream use getBytes("UTF-8") and to get back the String, use String(byte[],"UTF-8");

Upvotes: 2

Tobias
Tobias

Reputation: 7771

JVMs always use UTF in Strings (read this answer).

The critical part is the transmission of the String which is likely to happen on a byte-based stream. Converting a String to a byte[] actually requires you to specify the encoding. You should use utf-8 in most cases.

// On the client side
byte[] bytes = myString.getBytes("UTF-8");
serverStream.write(bytes);
// On the server side
byte[] bytes = /* read bytes */;
String myString = new String(bytes, "UTF-8");

I suggest using a DataOutputStream/DataInputStream which provide methods for charset-safe String transmissions.

Upvotes: 1

Related Questions