• Advertisement
Sign in to follow this  

C# write only stream

This topic is 2304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,
If I have a write-only stream such as TextWriter. How can I extract data from it, after they are written to? Pass the stream to some constructor or how?

Share this post


Link to post
Share on other sites
Advertisement
Can you explain your high level goal? One potential mechanism is to replace the stream with one that writes to a memory buffer. You can then write that buffer to the original stream, and inspect the contents of the memory buffer.

Share this post


Link to post
Share on other sites
thanks, the thing is, I need to log , in asp net, Response bytes that are sent as a response. The object Response has members : Output and OutputStream, theese are memory streams that are write-only. I can't imagine how I could get the bytes in these streams. Do you see any possible option?

Share this post


Link to post
Share on other sites
only option I can think of is to construct an instance of MemoryStream object with constructor parameter of the writeonly Stream. But the instance then still cant access reading like calls of itself , they all reise exceptions, such as Length property, Read() function and so on. the Output property is of Stream type, which is an abstract type, so the data type is more derived up. But eighter retyping, or constructing, the read like fields raise exceptions. From what I have red so far, the write only ability, enables the stream certain benefits .So maybe before accessing it, some post-processing needs to be done, for example, property Length is variable itself, and we do not know what size it detects, and in what affair/size.

Share this post


Link to post
Share on other sites

only option I can think of is to construct an instance of MemoryStream object with constructor parameter of the writeonly Stream. But the instance then still cant access reading like calls of itself , they all reise exceptions, such as Length property, Read() function and so on. the Output property is of Stream type, which is an abstract type, so the data type is more derived up. But eighter retyping, or constructing, the read like fields raise exceptions. From what I have red so far, the write only ability, enables the stream certain benefits .So maybe before accessing it, some post-processing needs to be done, for example, property Length is variable itself, and we do not know what size it detects, and in what affair/size.


Write only streams basically send data somewhere, unless you can access that "somewhere" there is nothing you can do after the data is allready written.

The HttpResponse subclass (Response is an instance of this) writes the data to a remote machine (The client) and as such cannot be read back after it has been written.

You have to store the data locally on the server (in ram or on disk) before you lose access to it.

This should be a decent solution (See bottom of post for a link to a TeeStream implementation), the Output (but not OutputStream) in Response should be able to be modified (in .Net 4.0, in earlier versions this property is read only) so using a TeeStream such as:
MemoryStream ms = new MemoryStream(); //This could also be another outputstream if you prefer to write directly to a logfile for example)
TeeOutputStream ts = new TeeOutputStream(Response.Output, ms);
Response.Output = ts;

after this you should be able to write to Response.Output normally and everything you write will also be written to your memorystream (Which can be read later).

If you can't modify Response.Output you can still create the TeeStream in the same way and simply write to ts instead of Response.Output. (Allthough this would require you to change your code in every place where you write to Response.Output at the moment) , If you're using things like the Redirect method this might not work at all (If Redirect works by writing to Output it will work, but if it doesn't use that stream internally then it won't work, i couldn't find anything in the docs about that)


http://www.cookcompu...tream-in-csharp

Share this post


Link to post
Share on other sites


Write only streams basically send data somewhere, unless you can access that "somewhere" there is nothing you can do after the data is allready written.


I believe Response.Write() call only writes data to a memory, and does not establish sending them over tcp to the client right away, instead , I expect the stream to be red by server and managed for transmitting to the remote end when document is after processing. Does it not work this way?

Share this post


Link to post
Share on other sites

[quote name='SimonForsman' timestamp='1320108063' post='4879077']
Write only streams basically send data somewhere, unless you can access that "somewhere" there is nothing you can do after the data is allready written.


I believe Response.Write() call only writes data to a memory, and does not establish sending them over tcp to the client right away, instead , I expect the stream to be red by server and managed for transmitting to the remote end when document is after processing. Does it not work this way?
[/quote]

Its most likely buffered yes, but thats an implementation detail. the interface doesn't let you access the internal buffers used by the TextWriter.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement