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:
- After I read, the position move to currentPosition+readed , Does the memStram.Length will changed?
- 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: ;
memStram.Position = 0; memStram.SetLength(0);
Thanks. Joseph
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 usingStream
.Yes,
SetLength(0)
is fine forMemoryStream
. It isn't necessarily fine in all cases (for example, it won't make much sense on aNetworkStream
).