Reuse io.stream object

  • Thread starter Thread starter Marc Thompson
  • Start date Start date
M

Marc Thompson

Hi there,

I have the following:

Dim objResponse As WebResponse = objReq.GetResponse
Dim objStream As IO.Stream

objStream = objResponse.GetResponseStream

Then, I read some Bytes from this stream and it works. I then go do some
other things, still withing the scope of objStream,
then come back to "objStream" and would like to start over reading all of
the Bytes in objStream. It won't let me reset the position=0 on
the objStream. How can I reset or reuse objStream without going through my
WebRequest/WebResponse again?

Thanks,
Marc.
 
"Marc Thompson" <(NO said:
Hi there,

I have the following:

Dim objResponse As WebResponse = objReq.GetResponse
Dim objStream As IO.Stream

objStream = objResponse.GetResponseStream

Then, I read some Bytes from this stream and it works. I then go do some
other things, still withing the scope of objStream,
then come back to "objStream" and would like to start over reading all of
the Bytes in objStream. It won't let me reset the position=0 on
the objStream. How can I reset or reuse objStream without going through my
WebRequest/WebResponse again?

One idea would be to read the entire stream into a byte array and then
create a MemoryStream from the byte array. Depending on the size of
your stream this may not be an option, but it would give you a stream
that you can reset to position zero.
 
Marc,

Why you want to reuse it and not just close it and create a new one.

I ask this because some people think it is better to reuse. When we think
about the immutable strings than you know than that is in my opinion a
little bit from the past. You can just create a new object and the memory of
the old one will be reused. However probably all on another place.

Better is in my opinion to close it as soon as possible.

Just my thought,

Cor
 
Thanks for the replies fellas,

I decided to save the original stream to disk (part of the requirements
anyways). I then read the stream in from disk and finish my other
operations.

Probably would be more efficient to make a duplicate of the stream in memory
but oh well.

Why won't MS give us a stream copy/clone function? It's not that many lines
of code!!!!

Thanks again,
Marc.
 
Back
Top