how to deal with the position in a c# stream

Posted by CapsicumDreams on Stack Overflow See other posts from Stack Overflow or by CapsicumDreams
Published on 2011-01-15T13:48:14Z Indexed on 2011/01/15 13:53 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

The (entire) documentation for the position property on a stream says:

  • When overridden in a derived class, gets or sets the position within the current stream.
  • The Position property does not keep track of the number of bytes from the stream that have been consumed, skipped, or both.

That's it. OK, so we're fairly clear on what it doesn't tell us, but I'd really like to know what it in fact does stand for. What is 'the position' for? Why would we want to alter or read it? If we change it - what happens?

In a pratical example, I have a a stream that periodically gets written to, and I have a thread that attempts to read from it (ideally ASAP). From reading many SO issues, I reset the position field to zero to start my reading. Once this is done:

  • Does this affect where the writer to this stream is going to attempt to put the data? Do I need to keep track of the last write position myself? (ie if I set the position to zero to read, does the writer begin to overwrite everything from the first byte?)
  • If so, do I need a semaphore/lock around this 'position' field (subclassing, perhaps?) due to my two threads accessing it?
  • If I don't handle this property, does the writer just overflow the buffer?

Perhaps I don't understand the Stream itself - I'm regarding it as a FIFO pipe: shove data in at one end, and suck it out at the other. If it's not like this, then do I have to keep copying the data past my last read (ie from position 0x84 on) back to the start of my buffer?

I've seriously tried to research all of this for quite some time - but I'm new to .NET. Perhaps the Streams have a long, proud (undocumented) history that everyone else implicitly understands. But for a newcomer, it's like reading the manual to your car, and finding out:

The accelerator pedal affects the volume of fuel and air sent to the fuel injectors. It does not affect the volume of the entertainment system, or the air pressure in any of the tires, if fitted.

Technically true, but seriously, what we want to know is that if we mash it to the floor you go faster..

© Stack Overflow or respective owner

Related posts about c#

Related posts about stream