2 Replies Latest reply: Oct 4, 2012 6:58 AM by 563912 RSS

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

      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.. 
      java.io.StreamCorruptedException: invalid stream header
           at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:764)
           at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
           at Test.main(Test.java:32)
      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
          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
            Oh Ya off cause.. :)

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