Skip to main content

Serializing an object not working properly - funky chars.

2 replies [Last post]
Joined: 2008-12-23

I have a java webservice.. I need to log the response before returning. I am trying to serialize as below:

ByteArrayOutputStream fos = new ByteArrayOutputStream();
ObjectOutputStream outStream = new ObjectOutputStream( fos );
outStream.writeObject( response );
return fos.toString();

But I see invalid characters all over. How should properly save this as an XML? Thanks,

Edit: And this object is Serializable.

Message was edited by: getstarted

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-12-23

any help?

Joined: 2005-01-14

You will need to encode the plain byte-array to make it fit into a XML-file as a String. Calling the ByteArrayOutputStream#toString is not an appropriate way for this. Try a Base64 encoder to make the byte[] from the Stream to a String and the appropriate Decoder to make a byte[] from the String. The byte[] can then be used as Input for De-Serialisiation.

I'm talking about <10 lines of code per direction.