How does changing the position of the BaseStream of a BinaryReader affect the current state of a BinaryWriter that uses the same BaseStream?

StackOverflow https://stackoverflow.com/questions/17124564

Question

I am working on a parser that watches a stream (probably from a NetworkStream). When certain data is seen on the source stream, a new MemoryStream is created and the relevant data from the source is written to it.

Then I pass the MemoryStream for parsing to another class method that parses the MemoryStream as a Stream. In this method a BinaryReader is created. When it goes to read the data there is none because the BinaryReader is actually at the end of the data.

The BinaryReader does not have a Position property or Seek method, so the underlining BaseStream position needs to be changed. Once the position is changed the stream can be parsed.

In this case we are not adding additional data so there is no problem. But the thought occurs to me that if a similar situation arrives and additional data is going to be written to the this might not work because the position value was changed behind its back.

I’m a bit fuzzy on the implications here.

Do the writer and reader work with copies of the BaseStream and its position or the original object allowing for corruption?

Does this mean that I need to create the reader at the same time I create the writer so that both start at the same spot then pass then reader to the method instead of the BaseStream? I think this might be a better practice.

Does the BinaryReader and Writer maintain their own position information? I'm thing not because the property isn't there. If not can you use them concurrently in the same thread?

Update #1: Based on an answer and comments that have since been withdrawn I think I need to make my confusion a little clearer. There is a BaseStream property in both BinaryWriter and BinaryReader. I thought it pointed to the stream object that was used to create the writer and reader. I’m starting to think it is just a worker object that is unique to both.

I don’t want to assume to much about the stream objects so that I remain open to multiple types of streams as a source.

Update #2: Now after running some test code I see that they are connected. When data is written it affects the position of the reader. I would have thought to be useful the reader would remain unaffected so that it could pick up where it left off, reading the next part of the stream. I envisioned something like this:

  • A data event occurs. The event causes data to be written by the writer.
  • At some point the reader works on some of the data in the stream.
  • Another event occurs causing more data to be written.
  • It is appended to the data that the reader already is working on.
  • The reader finishes its work including new data.

But based on the way the position works between the reader and writer this is not the way the stream is to be used,.

Maybe my conception problem is because my BaseStream is a MemoryStream and the rules are different than it would be with a NetworkStream. I was trying to keep the implementation details of the stream's source out of the reading class.

I guess at this point I have the answer to the question. Now I need to find information on using streams to do the type of thing that I have in my head.

Was it helpful?

Solution

I found that working with MemoryStream the Reader and Writer updated the position in the base stream. Therefore you cannot read a stream that is still being written without juggling copying and restoring of the position value. Not recommended.

I reworked things so that I could write an entire transaction to a MemoryStream pass it to another class. The create a new MemoryStream for the next transaction.

The base stream is not a derivation of the given stream, it is the actual stream.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top