Streaming programming
I need to learn more about streaming techniques. I am using biztalk and want to develop some custom pipelinecomponents. For perfor开发者_JAVA百科mance factors everything has to be in a stream based fashion. I receive a streamed message, but I want to do some replacements in the text, what I do now is:
string msg = "";
using(StreamReader r = new StreamReader(stream)){
msg = r.readToEnd();
}
//do replacements
//send stream away
StreamWriter...
As you see I break the stream when I execute r.readToEnd(). How can I edit the message in stream?
thx
You cannot. You can read parts of the message from the stream, replace what you want in each part and finally write processed part to another stream.
Using ReadToEnd is opposite to streaming concept. What I can suggest is that you should use:
using (StreamReader r = new StreamReader(stream))
using (StreamWriter w = new StreamWriter(someOutputStream))
{
string line = null;
while ((line = r.ReadLine()) != null)
{
line = DoReplacements(line);
w.WriteLine(line);
}
}
精彩评论