title: Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00
body: <p>I'm writing a java file-transfer app, and i have some troubles with deserialisation myself-defined class Message from Datagramms.</p>
  
  <p>Other topics at StackOverflow has similar issues, but i didn't found something helpful from there, so i'm sorry in advance if i missed it.</p>
  
  <p>so, class Message is:</p>
  
  <pre><code>import java.io.Serializable;
  
  public class Message implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      private int segmentID;
      private byte[] packet;
      private int bytesToWrite;
  
      public Message(){
          segmentID = -1;
      }
  
      public Message(int segmentID, byte[] packet, int bytesToWrite) {
          this.segmentID = segmentID;
          this.packet = packet;
          this.bytesToWrite = bytesToWrite;
      }
  
      public int getBytesToWrite() {
          return bytesToWrite;
      }
  
      public int getSegmentID() {
          return segmentID;
      }
  
      public byte[] getPacket() {
          return packet;
      }
  }
  </code></pre>
  
  <p>nothing special.
  i serialise it with </p>
  
  <pre><code>public byte[] serialize(Message obj) throws IOException
  {
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
      ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
      objectStream.flush();
      objectStream.writeObject(obj);
      objectStream.flush();
      return byteStream.toByteArray();
  }
  </code></pre>
  
  <p>and, actually, send it through datagram</p>
  
  <pre><code>msg = new byte[512];
  int bytesRead;
  bytesRead = fileReader.read(msg);//fileReader is FileInputStream
  Message message = new Message(segmentID, msg, bytesRead);
  byte[] test = serialize(message);
  datagramSocket.send(new DatagramPacket(test, test.length, 
      datagramSocket.getInetAddress(), datagramSocket.getPort()));
  </code></pre>
  
  <p>I recieve it from other place by</p>
  
  <pre><code>byte[] buffer = new byte[8192];
  DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
  socket.recieve(recievedPacket); //it's already connected by init packet
  receiveMSG = (Message) deserialize(receivedPacket.getData(), 
      receivedPacket.getOffset(), receivedPacket.getLength());
  </code></pre>
  
  <p>where (try/catch blocks are deleted here)</p>
  
  <pre><code>private Object deserialize(byte[] bytes, int offset, int length)
  {
      ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes, offset, length);
      ObjectInputStream objectStream = new ObjectInputStream(new BufferedInputStream(byteStream));
      Object tmpMsg = new Object();
          tmpMsg = objectStream.readObject();//here catches the EOFException
      return tmpMsg;
  }
  </code></pre>
  
  <p>why it can be? i did it according instructions at <a href="http://goo.gl/Rq1rMm" rel="nofollow">http://goo.gl/Rq1rMm</a><br>
  where and what i did wrong?
  i checked that initial messages passes (i have them, but here is only a little piece of my code) and datagram in receive is not null. Moreover, sent and received datagrams has equal length.
  Thanks in advance.</p>