Skip to main content

Serializing an object not working properly - funky chars.

2 replies [Last post]
getstarted
Offline
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 );
outStream.flush();
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.
getstarted
Offline
Joined: 2008-12-23

any help?

th_langer
Offline
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.

Regards,
TL