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

MemoryStream ReadBytes()

P: n/a
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.GetBy tes("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.GetBy tes("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!!!
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Nicolas" <ni**********@gmail.com> wrote in message
news:b8**************************@posting.google.c om...
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.GetBy tes("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.GetBy tes("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?
Nov 16 '05 #2

P: n/a
"Mike Schilling" <ms*************@hotmail.com> wrote in message news:<#S**************@tk2msftngp13.phx.gbl>...
"Nicolas" <ni**********@gmail.com> wrote in message
news:b8**************************@posting.google.c om...
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.GetBy tes("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.GetBy tes("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!!!!
Nov 16 '05 #3

P: n/a

"Nicolas" <ni**********@gmail.com> wrote in message
news:b8**************************@posting.google.c om...
"Mike Schilling" <ms*************@hotmail.com> wrote in message
news:<#S**************@tk2msftngp13.phx.gbl>...
"Nicolas" <ni**********@gmail.com> wrote in message
news:b8**************************@posting.google.c om...
> 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.GetBy tes("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.GetBy tes("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.
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.