title: Failed to read past end of stream
body: <p>So I'm trying to send some data over the network using UDP in Unity 5.3. Because the packages might be too big to be send at once, I tried to split them into smaller ones. (note: These packages get send at 30 times per second, so loss of a certain package really doesn't matter)</p>
  
  <p>What I basically did was:</p>
  
  <ol>
  <li>Create byte array</li>
  <li>Split into different, smaller, byte arrays</li>
  <li>Add byte array to serialized class with other information</li>
  <li>Convert serialized class to byte array</li>
  <li>Send, and vice-versa on the receiving client</li>
  </ol>
  
  <p>I receive the error "Failed to read past end of stream" when I try to deserialize a received byte array.</p>
  
  <p>Here's the code to Deserialize:</p>
  
  <pre><code>private static object ToObject(byte[] data)
  {
      var formatter = new BinaryFormatter();
      using (var stream = new MemoryStream(data))
      {
          stream.Seek(0, SeekOrigin.Begin);
          return formatter.Deserialize(stream);    // exception thrown here  
      }
  }
  </code></pre>
  
  <p>To Serialize:</p>
  
  <pre><code>private static byte[] ToByteArray(object source)
  {
      var formatter = new BinaryFormatter();
      using (var stream = new MemoryStream())
      {
          formatter.Serialize(stream, source);                
          return stream.ToArray();
      }
  }
  </code></pre>
  
  <p>Class: </p>
  
  <pre><code>[System.Serializable]
  public class Chunk
  {
      public short ID {get; set;}
      public short ChunkAmount {get; set;}
      public System.Int64 FrameID {get; set;}
      public byte[] ChunkData {get; set;}
  }
  </code></pre>
  
  <p>Any idea what could lead to this exception? </p>
  
  <p>You could save me a wig by figuring this out. So thanks in advance!</p>
  
  <p>EDIT:</p>
  
  <p>Provided functions that handle the serialization:</p>
  
  <pre><code>public static List&lt;byte[]&gt; GetChunks(List&lt;byte&gt; bytes, int maxChunkSize, System.Int64 frameID)
  {
      var list = new List&lt;byte[]&gt;(); 
  
      for (int i=0; i &lt; bytes.Count; i+= maxChunkSize) 
      { 
          list.Add(bytes.GetRange(i, Mathf.Min(maxChunkSize, bytes.Count - i)).ToArray()); 
      } 
  
      List&lt;byte[]&gt; serializedChunks = new List&lt;byte[]&gt;();
      for(int i = 0; i &lt; list.Count; i++)
      {
          Chunk c = new Chunk();
          c.ID = (short)i;
          c.ChunkData = list[i].ToArray();
          c.ChunkAmount = (short)list.Count;
          c.FrameID = frameID;
  
          serializedChunks.Add(ToByteArray(c));
      }
  
      return serializedChunks;
  }
  
  //Deserialized a chunk from byte array
  public static Chunk AssembleSingleChunk(byte[] serializedChunk)
  {
      return (Chunk)ToObject(serializedChunk);
  }
  
  public static byte[] AssembleChunks(List&lt;Chunk&gt; chunks)
  {
      List&lt;Chunk&gt; chunkList = new List&lt;Chunk&gt;(chunks);
      List&lt;Chunk&gt; sortedChunkList = chunkList.OrderBy(c =&gt; c.ID).ToList();
  
      List&lt;byte&gt; allBytes = new List&lt;byte&gt;();
      for (int i = 0; i &lt; sortedChunkList.Count; i++)
      {
          allBytes.AddRange(sortedChunkList[i].ChunkData);
      }
  
      return allBytes.ToArray();
  }
  </code></pre>