Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if serialized by encoding toString ISO-8859-1, how to unserialized #41

Open
stevenxuwoss opened this issue Apr 29, 2020 · 2 comments
Open

Comments

@stevenxuwoss
Copy link

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;
}

@tcalmant
Copy link
Owner

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

@stevenxuwoss
Copy link
Author

thanks very much, i'll hava a try with base64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants