File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Deserialization issue of class object received within XML message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Deserialization issue of class object received within XML message" Watch "Deserialization issue of class object received within XML message" New topic
Author

Deserialization issue of class object received within XML message

Mats Lintonsson
Greenhorn

Joined: Apr 05, 2014
Posts: 5
I receive an XML message (over an HttpURLConnection) that contains a serialized Java object. This is the received XML message:



What I want to do is to deserialize the Java object which is contained within the <Character> element. (The first four characters "aced" reveals that we are dealing with serialized content. So far so good.)

I'm using this code to grab the characters within <Character> and deserialize them into a Java object:



However, the above operation fails and I get this exception:



I cannot tell if the received serialized data is okay or not, so I really don't know if the problem is here in the deserialization or actually in the serialization (on the sending side where the Java object is serialized and packed into an XML message).

Any hints are very welcome.

Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41843
    
  63
I always thought that serialized objects were binary data, so to be transported via XML they would have to have been encoded first to become ASCII. I'd start by asking someone on the other side of this transmission how they generated that data.


Ping & DNS - my free Android networking tools app
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
Your character element contains a string representation of a hex dump so you can't read it as a String and then get call getBytes as this will get the bytes of each of the characters representing the hex values and not convert the pairs of characters to the byte value.

You need to iterate over the String converting each pair of characters to a byte value.
Mats Lintonsson
Greenhorn

Joined: Apr 05, 2014
Posts: 5
Thanks Ulf for your reply.

Actually, the "other side" is my code as well. My intention is to serialize the class object and tranform it into ASCII so that I can be sent in an XML message, exactly like you say.

If I look at the ASCII created, it do looks like a representation of a serialized class object:

aced = STREAM_MAGIC
0005 = STREAM_VERSION
73 = TC_OBJECT
etc

Reference: http://javapapers.com/core-java/java-serialization/

But somehow it gets messed up during the serialization anyway... :-(
Mats Lintonsson
Greenhorn

Joined: Apr 05, 2014
Posts: 5
Tony Docherty wrote:Your character element contains a string representation of a hex dump so you can't read it as a String and then get call getBytes as this will get the bytes of each of the characters representing the hex values and not convert the pairs of characters to the byte value.

You need to iterate over the String converting each pair of characters to a byte value.


Wow, this is certainly interesting information!

I definitely see what you mean, but I did not think of this at all. I will test this. It seems very likely to be the problem.

Big thank you! :-)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41843
    
  63
The serialization protocol does not actually seem to be binary, but something very close to UTF-8: http://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html. So it may just be possible to transfer it via XML, provided you pay attention to the encoding (your example uses UTF-16).
Mats Lintonsson
Greenhorn

Joined: Apr 05, 2014
Posts: 5
Ulf Dittmer wrote:The serialization protocol does not actually seem to be binary, but something very close to UTF-8: http://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html. So it may just be possible to transfer it via XML, provided you pay attention to the encoding (your example uses UTF-16).


Yes, it's important to pay attention to the encoding, otherwise bad things will happen. I think in my case, I had that part right, but messed when going from String to byte[], like Tony is pointing out. Will try this later this evening.
Mats Lintonsson
Greenhorn

Joined: Apr 05, 2014
Posts: 5
Ulf Dittmer wrote:The serialization protocol does not actually seem to be binary, but something very close to UTF-8: http://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html. So it may just be possible to transfer it via XML, provided you pay attention to the encoding (your example uses UTF-16).


By the way, thanks for the link to the serialization protocol. :-) Had not seen it before.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deserialization issue of class object received within XML message