You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I understand correctly, you want to transmit the serialized object as a String.
The ISO-8859-1 encoding should indeed ensure to have the equivalent of the underlying bytes without conversion nor error.
Note that internally, the String will have its "modified UTF-8" representation of the content: it is a Java String.
If you can, it would be preferable to use the byte array returned by byteArrayOutputStream.toByteArray(), as you won't have to handle encoding.
If you need the result to be a String, I recommend you to use a format that is not sensitive to the encoding, like Base64
public static String serialize(Object obj) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream;
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
//String string = byteArrayOutputStream.toString();
String string = byteArrayOutputStream.toString("ISO-8859-1");
objectOutputStream.close();
byteArrayOutputStream.close();
return string;
}
The text was updated successfully, but these errors were encountered: