title: How to binary (de)serialize object into/form string?
body: <p>I need serialize objects into String and deserialize.</p>
  
  <p>I readed sugestion on stackoverflow and make this code:</p>
  
  <pre><code>class Data implements Serializable {
  int x = 5;
  int y = 3;   
  }
  
  public class Test {
  public static void main(String[] args) {
  
      Data data = new Data();
  
      String out;
  
      try {
          // zapis
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ObjectOutputStream oos = new ObjectOutputStream(baos);
  
          oos.writeObject(data);
  
          out = new String(baos.toByteArray());
          System.out.println(out);
  
          // odczyt.==========================================
  
          ByteArrayInputStream bais = new ByteArrayInputStream(out.getBytes());
  
          ObjectInputStream ois = new ObjectInputStream(bais);
  
          Data d = (Data) ois.readObject();
  
          System.out.println("d.x = " + d.x);
          System.out.println("d.y = " + d.y);
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>but I get error:</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
  at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
  at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:298)
  at p.Test.main(Test.java:37)
  </code></pre>
  
  <p>Why?
  I expected:
  d.x = 5
  d.y = 3</p>
  
  <p>how to do in good way?
  Ah. I don't want to write this object in file. I have to have it in string format.</p>