MemoryStream ReadBytes()

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!!!
 
M

Mike Schilling

Nicolas said:
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?????

ReadBytes (like the other Read methods) reads at the MemoryStream's current
position. This is what you want; if every read reset the positon to 0, how
would you read a large MemoryStream in chunks?
 
M

Mike Schilling

Nicolas said:
Mike Schilling said:
Nicolas said:
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?????

ReadBytes (like the other Read methods) reads at the MemoryStream's
current
position. This is what you want; if every read reset the positon to 0,
how
would you read a large MemoryStream in chunks?

Mike your right about the ReadBytes method, I made a mistake writing
down my question, I really wanted to ask about the Read Method. The
ReadByte method reads byte by byte incrementing the position by one.
But the Read method does a calculation of the position (int num1 =
this._length - this._positionint num1 = this._length - this._position)
and if you don't set the position to 0 before you call this method it
won't return anything.

In my example a write the word testing (6 bytes), so the length and
position of the MemoryStream are 6, the result of the calculation is 0
so it returns nothing!!!!

Same answer. You want to be able to iterate though a large MemoryStream
using Read(), and you couldn't do that if it always reset the position.

Think of a MemoryStream as a magnetic tape. Write some stuff to the tape.
If you want to read what you've written, you need to rewind it first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top