tags:

views:

84

answers:

1

I've made the same mistake many times:

I've tried to use a Stream that is positioned in the end, consequently returning nothing.

What is the best way of designing the code involved in the stream loading and passing to avoid forgetting to rewind it?

It's is always better to have the stream just initializated but always load the data while being consumed to avoid this?

This is needed in not seekable streams, but what about memory or file streams? It is OK to have it loaded with data (from a file, of example) and having to rewind it prior to consuming it?

What do you think?

+1  A: 

If you found yourself reading the same Stream several times, then you must be "thinking" something wrong.

Rethink your use of Streams so you don't encounter this problem anymore.

camilin87
I agree completely. But, how should I rethink it? Which would be the best way of work with them instead?
Juan Calero
Well, I usually open the Stream and Close it as soon as possible. If I need the data on it I just cache it, in an Array, a List, or whatever I need.I need to call a method that needs a Stream, I just open it and pass it.Now about MemoryStreams, I think caching your data into some DataStructure is way easier to implement than seeking through a file.
camilin87