This discussion is archived
2 Replies Latest reply: Oct 4, 2012 4:58 AM by 563912 RSS

Serialization of java object to a string of byte..and the other way around

563912 Newbie
Currently Being Moderated
I am trying to serialize a object to a String of bytes, and then back again. I want to show the bytes in a web app, the user can copy the data from the web site and paste it into a another web site to deserialize the object again. BUT...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(bos);
            o.writeObject("Hello there !!");
            // Get the bytes of the serialized object
            byte[] buf = bos.toByteArray();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < buf.length; i++) {
                byte b = buf;
byte[] bytes = builder.toString().getBytes();

ByteArrayInputStream bis = new ByteArrayInputStream(buf);
//ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

ObjectInputStream inputStream = new ObjectInputStream(bis);
String input = (String) inputStream.readObject();
This works.. but if i use the out commented line instead of the line above I get.. invalid stream header
     at Test.main(

Edited by: Kristian Lind on Oct 4, 2012 11:41 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 1. Re: Serialization of java object to a string of byte..and the other way around
    sabre150 Expert
    Currently Being Moderated
    Object serialization creates a binary which you are trying to turn into a String using
                   byte b = buf;

    The append() will promote the byte 'b' to an 'int' and then write that as decimal to the StringBuilder.

    When you try to recover the serialized data using
              byte[] bytes = builder.toString().getBytes();
    it will treat each decimal digit as a byte and not the combination of several as a single byte.

    For example, assuming the byte b had value of 10 decimal then this would be written to the builder as two characters "10" but then converted back again to bytes as two bytes with value 49 and 48!

    The moral of the story - a String is never ever ever a valid container for binary data.

    To get round this you could use a Hex representation of each byte. Google for Jakarta Commons Codec for a suitable library.
  • 2. Re: Serialization of java object to a string of byte..and the other way around
    563912 Newbie
    Currently Being Moderated
    Oh Ya off cause.. :)

    With Hex.encodeHexString and Hex.decodeHex it works..



  • Correct Answers - 10 points
  • Helpful Answers - 5 points