tags:

views:

47

answers:

2

I have a class which accepts a stream as input (in the constructor). It surfaces content from this stream through various methods.

However, I don't want my object to be responsible for closing the stream -- that should be the responsibility of the caller. I therefore need to close my StreamReader inside my class, but I can't close the underlying stream.

Is this possible?

+3  A: 
Joel Coehoorn
I thought the finalizer of the stream reader/writer closed the stream...
Billy ONeal
@Billy: Finalizers should only dispose _native_ resources.
SLaks
@SLaks: Well I didn't write the StreamReader class....
Billy ONeal
+2  A: 

StreamReaders are designed to take complete and sole ownership of the underlying stream.

In particular, the StreamReader will arbitrarily read ahead in the stream to fill an internal buffer. (This bit me once)

If you need to share the stream, you probably shouldn't use a StreamReader at all.

SLaks
@SLacks: Generally speaking, I don't share the streamreader at all. The only place I need this is for unit testing a class representing a mount point.
Billy ONeal