Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Memory stream cannot read stream

Discussion in 'Scripting' started by rainboww, Dec 4, 2018.

  1. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    Hei, i have this custom nested dictionary to stream:

    Code (CSharp):
    1.  
    2.         public static Stream tostreamsmall(h.d3 inputdict)
    3.         {
    4.             string d3key = ""; string d2key = ""; string nullv="";
    5.             using (var stream = new MemoryStream())
    6.             using (var writer = new StreamWriter(stream)) {
    7.                 foreach (var keyaaa in inputdict) {
    8.                     d3key = keyaaa.Key;
    9.                     foreach (var keyaa in keyaaa.Value) {
    10.                         d2key = keyaa.Key;
    11.                         foreach (var keya in keyaa.Value) {
    12.                             if (d3key != keyaaa.Key) { writer.WriteLine(keyaaa.Key); d3key = keyaaa.Key; }
    13.                             else writer.WriteLine(nullv);
    14.                             if (d2key != keyaa.Key) { writer.WriteLine(keyaa.Key); d2key = keyaa.Key; }
    15.                             else writer.WriteLine(nullv);
    16.                             writer.WriteLine(keya.Key);
    17.                             writer.WriteLine(keya.Value);
    18.                         }
    19.                     }
    20.                 }
    21.                 writer.Flush();
    22.                 stream.Position = 0;
    23.                 return stream;
    24.             }
    25.         }
    26.  
    27.         public static h.d3 tohd3(Stream inputstream)
    28.         {
    29.             string d3key = ""; string d2key = "";
    30.             h.d3 dico = new h.d3();
    31.             pre( "inpuistreamlenght "+inputstream.ToString().Count());
    32.             pre("inpuistream " + inputstream.ToString());
    33.             //using (var stream = new MemoryStream(inputstream))
    34.             using (var reader = new StreamReader(inputstream)) {
    35.                 while (inputstream.Position < inputstream.Length) {
    36.                     var keyaaa = reader.ReadLine();
    37.                     var keyaa = reader.ReadLine();
    38.                     var keya = reader.ReadLine();
    39.                     var value = reader.ReadLine();
    40.                     if (keyaaa != "") { d3key = keyaaa; }
    41.                     if (keyaa != "") { d2key = keyaa; }
    42.                     dico.makeserialize(d3key, d2key, keya, value);
    43.                 }
    44.             }
    45.             dico.makeserializefinish();
    46.             return dico;
    47.         }
    Which is shorted with all not relevant lines cut:
    Code (CSharp):
    1.  
    2.         public static Stream tostreamsmall(h.d3 inputdict)
    3.         {
    4.             using (var stream = new MemoryStream())
    5.             using (var writer = new StreamWriter(stream)) {
    6.  
    7.                 writer.Flush();
    8.                 stream.Position = 0;
    9.                 return stream;
    10.             }
    11.         }
    12.  
    13.         public static h.d3 tohd3(Stream inputstream)
    14.         {
    15.  
    16.             using (var reader = new StreamReader(inputstream))
    17.  
    However i always get an error in tohd3 in this line: using (var reader = new StreamReader(inputstream)) { :
     
    Last edited: Dec 4, 2018
  2. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    I found my answer: I took the base code from the internet and the using statements seemed to dispose the stream.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    rainboww likes this.
  4. rainboww

    rainboww

    Joined:
    Jan 5, 2017
    Posts:
    125
    Last edited: Dec 4, 2018