views:

78

answers:

1

I got a HttpResponse.Filter filter that replaces text in the HTML.

I've created a class that derives from Stream and implemented the Write method:

public override void Write(byte[] buffer, int offset, int count)

I read all bytes from buffer and store them in a private StringBuilder, then I replace the text, and write the string back to the Stream.

But how can I determine when the stream is at the end of the stream. I.e. how do I determine when to write back the html (string) to the stream?

+1  A: 

The Read method returns the number of bytes read from the stream. It's important that you take care of this value, as it can be less than the number of bytes requested, especially if the source of the stream is something slow like an internet connection.

When the Read method returns zero, you have reached the end of the stream.

Guffa
I put a breakpoint in: public override int Read(byte[] buffer, int offset, int count)But it never gets hit
Erik
@Erik: I see... You don't actually read any data as you override the Write method... Then you can't detect the end of the stream, but you might be able to override the Close method and write out the result there.
Guffa
I solved it by writing in the override flush method. It works well. Thanks for your help!
Erik