Reputation: 1774
I have a MemoryStream /BinaryWriter , I use it as following:
memStram = new MemoryStream();
memStramWriter = new BinaryWriter(memStram);
memStramWriter(byteArrayData);
now to read I do the following:
byte[] data = new byte[this.BulkSize];
int readed = this.memStram.Read(data, 0, Math.Min(this.BulkSize,(int)memStram.Length));
My 2 question is:
memStram.Position = 0; memStram.SetLength(0);
Thanks. Joseph
Upvotes: 1
Views: 7002
Reputation: 186698
Upvotes: 3
Reputation: 1620
No the lenght should not change, and you can easily inspect that with a watch variable
i would use the using
statement, so the syntax will be more elegant and clear, and you will not forget to dispose it later...
Upvotes: 2
Reputation: 1062865
1: After I read, the position move to currentPosition+readed , Does the memStram.Length will changed?
Reading doesn't usually change the .Length
- just the .Position
; but strictly speaking, it is a bad idea even to look at the .Length
and .Position
when reading (and often: when writing), as that is not supported on all streams. Usually, you read until (one of, depending on the scenario):
Read
returns a non-positive value)I would also probably say: don't use BinaryWriter
. There doesn't seem to be anything useful that it is adding over just using Stream
.
2: I want to init the stream (like I just create it), can I do the following instead using Dispose and new again, if not is there any faster way than dispose&new:
Yes, SetLength(0)
is fine for MemoryStream
. It isn't necessarily fine in all cases (for example, it won't make much sense on a NetworkStream
).
Upvotes: 2