two objects contain same stream?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Goldman

    #1

    two objects contain same stream?

    Hi,

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

    Stream fs = new FileStream("out put.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.
  • Jon Skeet

    #2
    Re: two objects contain same stream?

    Daniel Goldman <hhh12347@yahoo .com> wrote:[color=blue]
    > Any advice about both a BinaryReader and BinaryWriter
    > containing same FileStream at same time? Like:
    >
    > Stream fs = new FileStream("out put.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.[/color]

    Hmmm... if you seek each time (not that you can with BinaryReader -
    I've got a SeekingBinaryRe ader 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 BinaryReaderWri ter - it's fairly
    straightforward to do - which would obviate some of the problems.
    [color=blue]
    > 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.[/color]

    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.GetStr ing to convert.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Daniel Goldman

      #3
      Re: two objects contain same stream?

      Jon Skeet <skeet@pobox.co m> wrote in message news:<MPG.19996 da5b88dad9198a2 6e@news.microso ft.com>...[color=blue]
      >
      > Hmmm... if you seek each time (not that you can with BinaryReader -
      > I've got a SeekingBinaryRe ader 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 BinaryReaderWri ter - it's fairly
      > straightforward to do - which would obviate some of the problems.[/color]

      Thanks for responding. Do I really need to get special
      SeekingBinaryRe ader and BinaryReaderWri ter classes?
      Can I just seek directly on the underlying stream, with
      br.BaseStream.S eek(), or is this unreliable?

      Can I possibly use br.BaseStream.F lush() 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?
      [color=blue]
      > Daniel Goldman <hhh12347@yahoo .com> wrote:[color=green]
      > > Any advice about both a BinaryReader and BinaryWriter
      > > containing same FileStream at same time? Like:
      > >
      > > Stream fs = new FileStream("out put.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.[/color]
      >[color=green]
      > > 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.[/color]
      >
      > 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.GetStr ing to convert.[/color]

      Comment

      Working...