Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stream / IO passing-close question

43 views
Skip to first unread message

Jeremiah Johnson

unread,
Jul 15, 2002, 1:15:14 PM7/15/02
to
I would like to pass a stream around to different methods
so that each method may extract the information that it
desires from the stream. Specifically, one method
extracts the header data and another method extracts the
content.

I am using a StreamReader to extract the header data and
BinaryReader to extract the content. It appears that when
the StreamReader closes, then stream itself is being
closed. The StreamReader documentation doesn't say that
is the expected functionality, but other readers suggest
that is what they do.

Is StreamReader explicitly closing the base stream when I
call Close() on the reader?

Is there a way that you know of for me to pass my stream
around without closing / reopening it? Specifically, I
want the stream position to remain in place for my next
read.

Thank you.

- jeremiah

Zane Keller

unread,
Jul 16, 2002, 4:50:32 AM7/16/02
to
Hi Jeremiah,

I am having exactly the same problem with BinaryReader and
BinaryWriter and have posted a similar question. My
concern is not so much maintaining the position, but
reopening the FileStream each time I want to use it.

If you find a solution would you let me know? Thanks.

Zane

>.
>

Jeremiah Johnson

unread,
Jul 16, 2002, 12:57:04 PM7/16/02
to
>If you find a solution would you let me know? Thanks.

Here is how I solved my problem. I hope that this is
helpful to you.

My methods pass a Stream and the original FileStream is
opened in the initial method caller. I open the
FileStream specifying that I want a zero-length buffer:

new FileStream(
directoryName + "\\" + dataFilename,
FileMode.Open,
FileAccess.Read,
FileShare.None,
1
);

In the method that reads the headers from the file, I use
a BinaryReader with ASCII encoding (because StreamReader
has a minimum buffer length of 128 bytes):

BinaryReader reader =
new BinaryReader( fileStream, Encoding.ASCII );

Since message headers end with a 'null' line, I wrote a
method to read lines similar to what StreamReader would
provide:

/// <summary>
/// Read a line from the binary reader.
/// </summary>
/// <param name="reader">
/// The binary reader to read bytes from.
/// </param>
/// <returns>
/// A string containing the line read from the
/// stream in the reader.
/// </returns>
public static string ReadLine( BinaryReader reader ) {

string line = null;

try {
char character = '\n';
while( (character = reader.ReadChar()) != '\n' ) {
line += character;
}
} catch( EndOfStreamException e ) {
// no problem, just return the line as it is
} catch( IOException e ) {
Log.Error(
"IO exception occurred during ReadLine: " +
e.Message );
}

return line;

}

Lastly, I make sure not to close the reader. The reader
Close method also closes the stream; whereas, the Dispose
method doesn't close the stream.

Good luck with your project!

- jeremiah

Zane Keller

unread,
Jul 17, 2002, 3:28:47 AM7/17/02
to
Jeremiah,
I think you gave me the clue I was looking for. I didn't
know that the Dispose method doesn't close the underlying
stream. Typically Close just calls Dispose so this is a
bit of a strange setup. However, if I can use Dispose to
finalize the object without closing the stream it will be
a workaround until they fix the framework.

Thanks.

Zane

>.
>

0 new messages