N
Nicolas
Hi Everybody, I'm working with the MemoryStream and I'm having
problems with the ReadBytes method. When I use it, this method never
returns bytes!!!!
MemoryStream sw = new MemoryStream();
sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
byte[] buffer = new byte[6];
byte[] result = sw.GetBuffer();
int a = sw.Read(buffer,2,4);
I used Reflector and I took a look at this method, here is part of the
code:
int num1 = this._length - this._position;
if (num1 > count)
{
num1 = count;
}
if (num1 <= 0)
{
return 0;
}
The curious part is that when you write bytes to the MemoryStream, the
length and the position are the same (at least in my example) so it
always returns 0.
To solve this I had to set the position of the MemoryStream to 0.
MemoryStream sw = new MemoryStream();
sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6);
sw.Position = 0
System.Text.StringBuilder sb = new System.Text.StringBuilder();
byte[] buffer = new byte[6];
byte[] result = sw.GetBuffer();
int a = sw.Read(buffer,2,4);
My question is: Is this necessary or should the ReadBytes method put
the MemoryStream position to 0?????
Thanks!!!
problems with the ReadBytes method. When I use it, this method never
returns bytes!!!!
MemoryStream sw = new MemoryStream();
sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
byte[] buffer = new byte[6];
byte[] result = sw.GetBuffer();
int a = sw.Read(buffer,2,4);
I used Reflector and I took a look at this method, here is part of the
code:
int num1 = this._length - this._position;
if (num1 > count)
{
num1 = count;
}
if (num1 <= 0)
{
return 0;
}
The curious part is that when you write bytes to the MemoryStream, the
length and the position are the same (at least in my example) so it
always returns 0.
To solve this I had to set the position of the MemoryStream to 0.
MemoryStream sw = new MemoryStream();
sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6);
sw.Position = 0
System.Text.StringBuilder sb = new System.Text.StringBuilder();
byte[] buffer = new byte[6];
byte[] result = sw.GetBuffer();
int a = sw.Read(buffer,2,4);
My question is: Is this necessary or should the ReadBytes method put
the MemoryStream position to 0?????
Thanks!!!