problem with memorystream and streamwriter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Heron

    problem with memorystream and streamwriter

    Hi,



    Could someone explain me why the following code doesn't work? The
    memorystream always remains with length 0.



    MemoryStream input = new MemoryStream();

    StreamWriter swriter = new StreamWriter(in put);

    swriter.Write(" test");

    swriter.Close() ;




  • Heron

    #2
    Re: problem with memorystream and streamwriter

    nm, found it :)
    Must user Flush() instead of Close() and keep the streamwriter open.

    "Heron" <nospam@nospam. comschreef in bericht
    news:uHHvkse9GH A.788@TK2MSFTNG P05.phx.gbl...
    Hi,
    >
    >
    >
    Could someone explain me why the following code doesn't work? The
    memorystream always remains with length 0.
    >
    >
    >
    MemoryStream input = new MemoryStream();
    >
    StreamWriter swriter = new StreamWriter(in put);
    >
    swriter.Write(" test");
    >
    swriter.Close() ;
    >
    >
    >
    >

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: problem with memorystream and streamwriter

      Heron <nospam@nospam. comwrote:
      Could someone explain me why the following code doesn't work? The
      memorystream always remains with length 0.
      >
      MemoryStream input = new MemoryStream();
      StreamWriter swriter = new StreamWriter(in put);
      swriter.Write(" test");
      swriter.Close() ;
      Could you post a short but complete program which demonstrates the
      problem?

      See http://www.pobox.com/~skeet/csharp/complete.html for details of
      what I mean by that.

      Here's a program which works:

      using System;
      using System.IO;

      class Test
      {
      static void Main()
      {
      MemoryStream input = new MemoryStream();
      StreamWriter swriter = new StreamWriter(in put);
      swriter.Write(" test");
      swriter.Close() ;
      Console.WriteLi ne (input.ToArray( ).Length);
      }
      }

      Note that you can't access the Length property after the MemoryStream
      is closed, which the StreamWriter will do when it's closed.

      If you want to access the Length property while the StreamWriter is
      still open, you should call Flush() first:

      using System;
      using System.IO;

      class Test
      {
      static void Main()
      {
      MemoryStream input = new MemoryStream();
      StreamWriter swriter = new StreamWriter(in put);
      swriter.Write(" test");
      swriter.Flush() ;
      Console.WriteLi ne (input.Length);
      swriter.Close() ;
      }
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Heron

        #4
        Re: problem with memorystream and streamwriter

        Hi Jon,

        Thanks for replying. Below is a complete program as requested.
        My problem was in the encrypt method which is solved now but i'm still
        having a problem with the decrypt method, the sreader.ReadToE nd(); method
        throws me a "Padding is invalid and cannot be removed." error.

        using System;
        using System.IO;
        using System.Security .Cryptography;
        using System.Collecti ons.Generic;
        using System.Text;
        namespace testconsole
        {
        class Program
        {
        static void Main(string[] args)
        {
        byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        13, 14, 15, 16 };
        byte[] iv = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
        14, 15, 16, 17 };
        MyRijndael rn = new MyRijndael(key) ;
        string enc = rn.Encrypt("abc defghijklmnopqr stuvwxyz", iv);
        string dec = rn.Decrypt(enc, iv);
        }

        public class MyRijndael
        {
        private byte[] _key;
        public MyRijndael(byte[] key)
        {
        this._key = key;
        }
        public string Encrypt(string input, byte[] iv)
        {
        MemoryStream output = new MemoryStream();
        Rijndael rn = Rijndael.Create ();
        CryptoStream scrypt = new CryptoStream(ou tput,
        rn.CreateEncryp tor(this._key, iv), CryptoStreamMod e.Write);
        scrypt.FlushFin alBlock();
        StreamWriter swriter = new StreamWriter(sc rypt);
        swriter.Write(i nput);
        swriter.Flush() ;
        byte[] buffer = output.ToArray( );
        swriter.Close() ;
        scrypt.Close();
        output.Close();
        return Convert.ToBase6 4String(buffer) ;
        }

        public string Decrypt(string input64, byte[] iv)
        {
        MemoryStream input = new
        MemoryStream(Co nvert.FromBase6 4String(input64 ));
        Rijndael rn = Rijndael.Create ();
        CryptoStream scrypt = new CryptoStream(in put,
        rn.CreateDecryp tor(this._key, iv), CryptoStreamMod e.Read);
        StreamReader sreader = new StreamReader(sc rypt);
        string res = sreader.ReadToE nd(); //<-- error
        "Padding is invalid and cannot be removed."
        sreader.Close() ;
        scrypt.Close();
        input.Close();
        return res;
        }
        }
        }
        }



        "Jon Skeet [C# MVP]" <skeet@pobox.co mschreef in bericht
        news:MPG.1fa5d2 216862968e98d56 d@msnews.micros oft.com...
        Heron <nospam@nospam. comwrote:
        >Could someone explain me why the following code doesn't work? The
        >memorystream always remains with length 0.
        >>
        >MemoryStream input = new MemoryStream();
        >StreamWriter swriter = new StreamWriter(in put);
        >swriter.Write( "test");
        >swriter.Close( );
        >
        Could you post a short but complete program which demonstrates the
        problem?
        >
        See http://www.pobox.com/~skeet/csharp/complete.html for details of
        what I mean by that.
        >
        Here's a program which works:
        >
        using System;
        using System.IO;
        >
        class Test
        {
        static void Main()
        {
        MemoryStream input = new MemoryStream();
        StreamWriter swriter = new StreamWriter(in put);
        swriter.Write(" test");
        swriter.Close() ;
        Console.WriteLi ne (input.ToArray( ).Length);
        }
        }
        >
        Note that you can't access the Length property after the MemoryStream
        is closed, which the StreamWriter will do when it's closed.
        >
        If you want to access the Length property while the StreamWriter is
        still open, you should call Flush() first:
        >
        using System;
        using System.IO;
        >
        class Test
        {
        static void Main()
        {
        MemoryStream input = new MemoryStream();
        StreamWriter swriter = new StreamWriter(in put);
        swriter.Write(" test");
        swriter.Flush() ;
        Console.WriteLi ne (input.Length);
        swriter.Close() ;
        }
        }
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: problem with memorystream and streamwriter

          Heron <nospam@nospam. comwrote:
          Thanks for replying. Below is a complete program as requested.
          My problem was in the encrypt method which is solved now but i'm still
          having a problem with the decrypt method, the sreader.ReadToE nd(); method
          throws me a "Padding is invalid and cannot be removed." error.
          You're calling FlushFinalBlock before you've actually written any data.
          Call it after calling Flush on the StreamWriter instead.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...