473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

connecting streams ...

Hello

how do I connect streams in c# ?

Imagine the followung scenario: I have a StreamWriter that writes Text to a
Stream. How can I tell that Stream to pass that Data to another Stream
(connect it to another output Stream) ? In java I am used to connect
streams to each other via thier Constructors.
For example:
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("soc.txt"),
"UTF-8"));

Or even more interesting: What if I wanted that Stream to pass its Data to
multiple other Streams. What do I have to do in that case ?

<> heh, didn't you ever watch Ghostbusters?
<> you can't cross the streams

.... does that mean ... ?


Nov 16 '05 #1
2 3993
bonk <sc******************@gmx.de> wrote:
how do I connect streams in c# ?

Imagine the followung scenario: I have a StreamWriter that writes Text to a
Stream. How can I tell that Stream to pass that Data to another Stream
(connect it to another output Stream) ? In java I am used to connect
streams to each other via thier Constructors.
For example:
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("soc.txt"),
"UTF-8"));
In this case, you'd just use new StreamWriter (new FileStream(...)).
There's no need for a BufferedWriter particularly, although you could
always use BufferedStream in there (between the StreamWriter and the
FileStream) if you want.
Or even more interesting: What if I wanted that Stream to pass its Data to
multiple other Streams. What do I have to do in that case ?


I don't believe there's a stream class which writes to multiple
streams, but it wouldn't be particularly hard to write one - just have
a collection of streams, and any time data is written to the single
stream, write it to all the others.

--
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
> how do I connect streams in c# ?
(connect it to another output Stream) ? In java I am used to connect
streams to each other via thier Constructors.
For example:
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("soc.txt"),
"UTF-8"));
Example of connecting multiple streams. In this case, I don't think the
buffered stream or the fs provide much value as you could just use the sr
only and save indirection to two other streams. If one was a zip stream or
crypto stream, then value is apparent.

using(FileStream fs = new FileStream(@"c:\myfile.txt", FileMode.Open))
using(BufferedStream bs = new BufferedStream(fs))
using(StreamReader sr = new StreamReader(bs))
{
string line = null;
while((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Or even more interesting: What if I wanted that Stream to pass its Data to
multiple other Streams. What do I have to do in that case ?


Create your own stream by inheritence of stream. Then allow your stream to
Add stream instances to it of the type you can write to. Then override
write method to write to all streams in your collection.

--
William Stacey, MVP
http://mvp.support.microsoft.com
Nov 16 '05 #3

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

Similar topics

8
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? ...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
11
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed....
2
by: Peter Rilling | last post by:
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...
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
2
by: Abhishek | last post by:
what are the STDUPDATE, STDERR, STDOUT and STDIN streams and how does one access these streams in C language. I am aware of the function fprintf(FILE *fp, char * format, char *s) which puts the...
1
by: woessner | last post by:
This is not a really well-formed question so please bear with me. I have a very large binary file (tens of GB). I would like to write a quick program to take a contiguous piece of the file and...
5
by: Bruce | last post by:
Hello I am building a C# app that creates anywhere from 10 to 100 connections to a specified server and sends 1000s of TCP requests and processes the responses. (it is a stress tool) I planned...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.