By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

problem with memorystream and streamwriter

P: n/a
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(input);

swriter.Write("test");

swriter.Close();


Oct 22 '06 #1
Share this Question
Share on Google+
4 Replies


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

"Heron" <no****@nospam.comschreef in bericht
news:uH*************@TK2MSFTNGP05.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(input);

swriter.Write("test");

swriter.Close();


Oct 22 '06 #2

P: n/a
Heron <no****@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(input);
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(input);
swriter.Write("test");
swriter.Close();
Console.WriteLine (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(input);
swriter.Write("test");
swriter.Flush();
Console.WriteLine (input.Length);
swriter.Close();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #3

P: n/a
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.ReadToEnd(); method
throws me a "Padding is invalid and cannot be removed." error.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.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("abcdefghijklmnopqrstuvwxyz", 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(output,
rn.CreateEncryptor(this._key, iv), CryptoStreamMode.Write);
scrypt.FlushFinalBlock();
StreamWriter swriter = new StreamWriter(scrypt);
swriter.Write(input);
swriter.Flush();
byte[] buffer = output.ToArray();
swriter.Close();
scrypt.Close();
output.Close();
return Convert.ToBase64String(buffer);
}

public string Decrypt(string input64, byte[] iv)
{
MemoryStream input = new
MemoryStream(Convert.FromBase64String(input64));
Rijndael rn = Rijndael.Create();
CryptoStream scrypt = new CryptoStream(input,
rn.CreateDecryptor(this._key, iv), CryptoStreamMode.Read);
StreamReader sreader = new StreamReader(scrypt);
string res = sreader.ReadToEnd(); //<-- error
"Padding is invalid and cannot be removed."
sreader.Close();
scrypt.Close();
input.Close();
return res;
}
}
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP************************@msnews.microsoft.c om...
Heron <no****@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(input);
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(input);
swriter.Write("test");
swriter.Close();
Console.WriteLine (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(input);
swriter.Write("test");
swriter.Flush();
Console.WriteLine (input.Length);
swriter.Close();
}
}

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

Oct 22 '06 #4

P: n/a
Heron <no****@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.ReadToEnd(); 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.