title: StreamCorruptedException: invalid stream header
body: <p>I have a class Serializer </p>
  
  <pre><code>public class Serializer {
  
  private Serializer() {}
  
  public static byte[] serialize(Object obj) throws IOException {
      ByteArrayOutputStream b = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(b);
      out.writeObject(obj);
      return b.toByteArray();
  }
  
  public static Object deserialize(byte [] bytes) throws IOException, ClassNotFoundException {
      if (bytes == null) return null;
      ByteArrayInputStream b = new ByteArrayInputStream(bytes);
      ObjectInputStream in = new ObjectInputStream(b);
      return in.readObject();
  }
  }
  </code></pre>
  
  <p>My client sends data through</p>
  
  <pre><code>private static DataInputStream in;
  private static DataOutput out;
  ...
  out.writeInt(bytes.length);
  out.write(bytes);
  </code></pre>
  
  <p>I read it this way</p>
  
  <pre><code>int length = in.readInt();
              if (length &gt; 0) {
                  byte[] bytes = new byte[length];
                  in.readFully(bytes);
                  byte[] result = sp.processInput(bytes);
              }
  </code></pre>
  
  <p>And then when I try to deserialize my Person object(serializable) it throws me an error</p>
  
  <pre><code>Person person = (Person) Serializer.deserialize(bytes);
  java.io.StreamCorruptedException: invalid stream header: 03ACED00
  at model.Serializer.deserialize(Serializer.java:22)
  </code></pre>
  
  <p>Person class:</p>
  
  <pre><code>public class Person implements Serializable {
  private String creationDate;
  private String name;
  private String birthDate;
  private String city;
  private String phoneNumber;
  private String email;
  private String university;
  private String place;
  private String reason;
  private SerializableImage photo;
  private boolean attended;
  </code></pre>
  
  <p>I can't use ordinary image because it's not serializable, so I came up with this class.</p>
  
  <p>SerializableImage class</p>
  
  <pre><code>public class SerializableImage implements Serializable {
  private int width, height;
  private int[][] data;
  
  public SerializableImage(Image image) {
      setImage(image);
  }
  
  public void setImage(Image image){...}
  public Image getImage() {...}
  </code></pre>
  
  <p>I cannot use ObjectInputStream in the first place because my byte array contains first byte saying what to do with the desirealized object.</p>
  
  <p>Everything was okey when I sent strings and other core objects. </p>
  
  <p>Can you please point out where I have the error?</p>