Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to serialize Images using XStream?

 
Mimi Tam
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

XStream is working great for me. Now I need to serialize/deserialize an image file (e.g. .png, .img, etc) which is inside my XML HTTP message. I am using Hibernate 3.0 (Annotations).

I did some digging for the info but I couldn't find examples or mentionings on how to serialize, and few for deserialize.

Can someone give me some info/examples on how to do them, please?

Currently, I have this code segment:

The returned string was formatted in XML simply as a string. I have *.png files embedded in the XML content and I need to send them as is in binary (not the file name) properly between 2 XML tags and the rest string.

Looking forward to hearing from someone.

Many Thanks...Mimi







Many Thanks...Mimi
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you cant include arbitrary binary directly in XML, you must encode it in legal XML characters.

The Apache Commons Codec library includes base64 encoding classes. With Base64 encoding, 3 bytes become 4 characters so the overhead is not too bad.

Bill
 
Mimi Tam
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your response, Bill.

I was thinking more on the line of using @Lob, byte[] for my image fields on my POJOs and hoping that they will be serialized/deserialized to MySQL and become longblob type.

Since you cant include arbitrary binary directly in XML, you must encode it in legal XML characters


Are you referring to the CLOBs and BLOBs and from converting BLOBs to Byte[] and back, that kind of conversion? That is what I have in mind but I am seeking an example of how this is done in a more complete context, instead of 2/3 line code segments.

Sorry, I wasn't too clear.

Any help will be greatly appreciated.


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you referring to the CLOBs and BLOBs and from converting BLOBs to Byte[] and back, that kind of conversion?


No, I a referring to the fact that the byte[] of image data can not be inserted in a SOAP message directly because there are many byte values which are not legal as XML characters.

I have NO idea what facilities Hibernate 3.0 provides so you are on your own there.

Bill
 
Mimi Tam
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so very much for your reply and clarification, Bill. I really appreciate it.

I finally got it working using Hibernate 3 Annotations and XStream. The code required is very little, very clean and concise. It was my oversight of error in my code and I created the problem for myself.

Thanks again...Mimi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic