472,353 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Is there any iterator-like constructs for Streams?

One nice thing about collections and arrays is that they implement the
IEnumerator and IEnumerable interfaces which allow for more then one
iterator to walk the list of items without affecting the position of the
other iterators. Is there a similar thing with streams? Is there anything
in the framework that wraps a stream in such a way that the stream position
is decoupled from the stream itself?
Nov 16 '05 #1
2 1725
Peter Rilling <pe***@nospam.rilling.net> wrote:
One nice thing about collections and arrays is that they implement the
IEnumerator and IEnumerable interfaces which allow for more then one
iterator to walk the list of items without affecting the position of the
other iterators. Is there a similar thing with streams? Is there anything
in the framework that wraps a stream in such a way that the stream position
is decoupled from the stream itself?


Bear in mind that not every stream is seekable. Once you've read from a
network connection, for instance, you can't go and rewind that. The
stream would have to buffer everything. It's doable, no doubt, but I
don't believe it's provided in the framework.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Peter Rilling <pe***@nospam.rilling.net> wrote:
One nice thing about collections and arrays is that they implement the
IEnumerator and IEnumerable interfaces which allow for more then one
iterator to walk the list of items without affecting the position of the
other iterators. Is there a similar thing with streams? Is there
anything
in the framework that wraps a stream in such a way that the stream
position
is decoupled from the stream itself?
Bear in mind that not every stream is seekable. Once you've read from a
network connection, for instance, you can't go and rewind that. The
stream would have to buffer everything. It's doable, no doubt, but I
don't believe it's provided in the framework.


Definatly not something that will work in general, although I could see
value in designing the object model to support it. Perhaps instead of basing
it on Stream, the framework should have defined a subclass SeekableStream or
whatnot which would support seeking, length changing, etc instead of
defining Seek on Stream(from which FileStream and MemoryStream, for example,
would derive). It'd not be difficult to write this kind of implementation
using streams that can seek, you would just have to use the CanSeek property
to determine seekability instead of relying on the compiler to catch the
mistake. Infact that might be a fun thing to write to play with C#
iterators, I've been looking for a project to play with them on anyway(ought
to be easy to write an iterator which produces a generic IEnumerator which
reads data structures from a stream, not so useful but something to do).

Its unfortunate that streams were designed as they were, with the three can
properties instead being inheritance based, but its understandable due to
the lack of multiple inheritance(although, the C++ implementation of streams
iss pretty horrible, much of it due to overuse of MI). I suppose an
interface based system could have been written, but it would have been ugly.
Atleast a seperation of a basic stream which reads and writes linearly and a
stream that supports seeking would have been appropriate, IMHO. Streams are,
at the most, *often* seekable but the non-seekable case is common enough(and
annoying enough) that I have trouble attributing it as a standard behaviour
of a stream even if I can't think of a library that doesn't support it.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> ...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> ...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o ...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. ...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.