two objects contain same stream?

D

Daniel Goldman

Hi,

Any advice about both a BinaryReader and BinaryWriter
containing same FileStream at same time? Like:

Stream fs = new FileStream("output.dbf", FileMode.Create);
BinaryReader br = new BinaryReader(fs);
BinaryWriter bw = new BinaryWriter(fs);
/* BinaryWriter writes, flushes, etc */
/* BinaryReader seeks, reads, etc */
/* BinaryWriter seeks, writes, etc */

Same question for StreamReader and StreamWriter containing
same FileStream at same time.

Also, is it asking for trouble to have combination of text
and binary writers (SW and BW) contain and fiddle with same
FS at the same time? I could change some file formats instead.

FS object doesn't know which objects contain it, doesn't care
how many containing objects call methods on it. As long as I
make sure no other process is accessing the Stream when it is
being written to, there will be no problem. Correct?

Any books/tutorials that discuss multiple objects containing
same stream at same time?

Thanks,
Daniel Goldman
Seattle, WA

Background - I'm porting a C program to C#. It reads and
writes various binary and text formats. I want to make C#
non-interface program flow parallel existing C logic (I'm not
discarding the C program). I've read and experimented a fair
amount with System.IO, but I have not found mention of
multiple objects containing same stream, which would make
it easier to mimic C IO logic. It seems to work, but I'd
like to know more before going further.
 
J

Jon Skeet

Daniel Goldman said:
Any advice about both a BinaryReader and BinaryWriter
containing same FileStream at same time? Like:

Stream fs = new FileStream("output.dbf", FileMode.Create);
BinaryReader br = new BinaryReader(fs);
BinaryWriter bw = new BinaryWriter(fs);
/* BinaryWriter writes, flushes, etc */
/* BinaryReader seeks, reads, etc */
/* BinaryWriter seeks, writes, etc */

Same question for StreamReader and StreamWriter containing
same FileStream at same time.

Hmmm... if you seek each time (not that you can with BinaryReader -
I've got a SeekingBinaryReader if you'd like) it should be okay. Bear
in mind that readers may buffer data, so you need to make sure you seek
through the enclosing objects rather than directly on the underlying
stream, otherwise it might get confused. If I were you, I'd be quite
tempted to write my own BinaryReaderWriter - it's fairly
straightforward to do - which would obviate some of the problems.
Also, is it asking for trouble to have combination of text
and binary writers (SW and BW) contain and fiddle with same
FS at the same time? I could change some file formats instead.

While I would generally consider it a bad idea, it's not as problematic
as with readers, as you can simply flush after each operation.

Personally I would try to make sure that in your binary format, you
know which sections represent text, read them as a byte array, and then
use Encoding.GetString to convert.
 
D

Daniel Goldman

Jon Skeet said:
Hmmm... if you seek each time (not that you can with BinaryReader -
I've got a SeekingBinaryReader if you'd like) it should be okay. Bear
in mind that readers may buffer data, so you need to make sure you seek
through the enclosing objects rather than directly on the underlying
stream, otherwise it might get confused. If I were you, I'd be quite
tempted to write my own BinaryReaderWriter - it's fairly
straightforward to do - which would obviate some of the problems.

Thanks for responding. Do I really need to get special
SeekingBinaryReader and BinaryReaderWriter classes?
Can I just seek directly on the underlying stream, with
br.BaseStream.Seek(), or is this unreliable?

Can I possibly use br.BaseStream.Flush() to reliably take care
of the reader buffering data? The documentation for Flush says:

"If CanSeek is true and data was previously copied from the
file to the buffer for reading, the current position within
the file is decremented by the number of unread bytes in the
buffer. The buffer is then cleared."

CanSeek is true. What does it mean "the buffer is then cleared"?
Cleared to where? Hopefully not to the bit bucket. Anybody know?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top