Question by 
               spinnerbox · Mar 09, 2016 at 11:09 AM · 
                unity 5serializationtestingstreamdeserialization  
              
 
              Cannot read past end of stream, MemoryStream
I have this test:
          [Test]
         public void FileOpenTest_ ()
         {
             // arrange
             ISaveLoadGameData slgdStub = Substitute.For<SaveLoadGameDataStub>();
 
             // act
             GameData data = slgdStub.FileOpen();
             
             // assert
             Assert.AreEqual(data.experience, GameDataDefaults.DEFAULT_EXPERIENCE);
         }
         public GameData FileOpen ()
         {
             bf = new BinaryFormatter();
             ms = DefaultsGameDataStream();
             data = (GameData)bf.Deserialize(ms);
             
             ms.Close();
             
             return data;
         }
         public MemoryStream DefaultsGameDataStream ()
         {
              MemoryStream defaultsMs = new MemoryStream();
             BinaryWriter defaultsBw = new BinaryWriter(defaultsMs);
             
             defaultsBw.Write(GameDataDefaults.DEFAULT_EXPERIENCE);
             defaultsBw.Write(GameDataDefaults.DEFAULT_SCORE);
             defaultsBw.Write(GameDataDefaults.DEFAULT_WIN_PERCENT);
 
                          return defaultsMs;
                 }
With this error:
 FileOpenTest_ (0.14s)
 ---
 System.IO.EndOfStreamException : Failed to read past end of stream.
 ---
 at System.IO.BinaryReader.ReadByte () [0x00047] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/BinaryReader.cs:293
 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.ReadBinaryHeader (System.IO.BinaryReader reader, System.Boolean& hasHeaders) [0x00000] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:267
 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00039] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:161
 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136
 at Assets.UnityTestTools.UnitTesting.Editor.AritmetikoUnitTests.Stubs.SaveLoadGameDataStub.FileOpen () [0x00017] in D:\svnrepo\trunk\Aritmetiko\Assets\UnityTestTools\UnitTesting\Editor\AritmetikoUnitTests\Stubs\SaveLoadGameDataStub.cs:208
 at Assets.UnityTestTools.UnitTesting.Editor.AritmetikoUnitTests.SaveLoadGameDataTests.FileOpenTest_ () [0x0000c] in D:\svnrepo\trunk\Aritmetiko\Assets\UnityTestTools\UnitTesting\Editor\AritmetikoUnitTests\SaveLoadGameDataTests.cs:429
Not sure why it gives an error. Any ideas?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by spinnerbox · Mar 09, 2016 at 11:31 AM
I rewrote FileOpen() to:
         public GameData FileOpen ()
         {
             bf = new BinaryFormatter();
             ms = new MemoryStream(DefaultsGameDataStream());
             BinaryReader br = new BinaryReader(ms);
             data = new GameData();
             data.experience = br.ReadSingle();
             data.score = br.ReadSingle();
             data.winPercent = br.ReadSingle();
             
             ms.Close();
             
             return data;
         }
Your answer
 
 
             Follow this Question
Related Questions
How can I separate serializable class into its own file? 1 Answer
"SerializationException: Unexpected binary element: 0" on Deserialization of struct 0 Answers
Recursive serialization error? 0 Answers
Argument Exception: J SON must represent an object type. Serialization list of objects 0 Answers
Testing Duration With Unit Tests 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                