Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does changing the position of the BaseStream of a BinaryReader affect the current state of a BinaryWriter that uses the same BaseStream?
    primarykey
    data
    text
    <p>I am working on a parser that watches a stream (probably from a <code>NetworkStream</code>). When certain data is seen on the source stream, a new <code>MemoryStream</code> is created and the relevant data from the source is written to it.</p> <p>Then I pass the <code>MemoryStream</code> for parsing to another class method that parses the <code>MemoryStream</code> as a <code>Stream</code>. In this method a <code>BinaryReader</code> is created. When it goes to read the data there is none because the <code>BinaryReader</code> is actually at the end of the data.</p> <p>The BinaryReader does not have a <code>Position</code> property or <code>Seek</code> method, so the underlining <code>BaseStream</code> position needs to be changed. Once the position is changed the stream can be parsed.</p> <p>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.</p> <p>I’m a bit fuzzy on the implications here.</p> <p>Do the writer and reader work with copies of the <code>BaseStream</code> and its position or the original object allowing for corruption?</p> <p>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 <code>BaseStream</code>? I think this might be a better practice.</p> <p>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?</p> <p><strong>Update #1:</strong> 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 <code>BaseStream</code> property in both <code>BinaryWriter</code> and <code>BinaryReader</code>. 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.</p> <p>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.</p> <p><strong>Update #2:</strong> 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:</p> <ul> <li>A data event occurs. The event causes data to be written by the writer.</li> <li>At some point the reader works on some of the data in the stream.</li> <li>Another event occurs causing more data to be written.</li> <li>It is appended to the data that the reader already is working on.</li> <li>The reader finishes its work including new data.</li> </ul> <p>But based on the way the position works between the reader and writer this is not the way the stream is to be used,.</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload