Re: Output stream deleting last few bytes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Mike P2 wrote:
I made a Stream-inheriting class that just removes the tabs (actually
the 4 spaces VS prefers to use) from the beginning of lines and empty
lines. At first I was having trouble with it adding a null character
in the middle of the output. I think it was a null character, Firefox
displays it as a '?' and the W3C validator says it's a non-sgml
character '0'. I changed the code around a bit and now the only
problem is that it (on some pages of different lengths) cuts off a few
characters at the end of the file.

For example, on one page it turns "</html>" to "</htm", and on another
page it only cuts off the '>'. One page of much greater length (I
don't know if that's significant), I don't notice anything missing or
different (I might be missing something in the middle).

Could this be a common mistake? I've never used streams in ASP.net
before.

Here are two relevant methods in my VB class:
..........................................................

Public Sub New(ByVal orig As Stream)

'keep a copy of other filters already applied so
'this one is just added to those
_origStream = orig

End Sub

Public Overrides Sub Write(ByVal stuff() As Byte, ByVal offset As
Integer, ByVal count As Integer)

'convert data to a string so we can work with it
Dim outgoing As String
outgoing = System.Text.Encoding.UTF8.GetString(stuff)

Oops! You are converting the entire buffer into a string, ignoring the offset and count parameters. There is an overload of GetString that takes offset and count that you can use.

outgoing = Regex.Replace(outgoing, "\n\s+", ControlChars.Lf)
'this one is only useful for the first chunk:
outgoing = Regex.Replace(outgoing, "^\s+<!DOC", "<!DOC")

'convert to array of bytes again and give it back
stuff = System.Text.Encoding.UTF8.GetBytes(outgoing)
_origStream.Write(stuff, 0, stuff.GetLength(0))

End Sub
..........................................................

Of coarse, _origStream is a Stream class field. Commenting out the two
replace operations does not help. I assume my problem is either in how
I'm calling the Stream::Write() method or converting the data between
an array and a string, but I don't see what exactly could be the
problem.

Thanks,
Mike PII


How are you handling closing and disposing of the stream? I think that you might be missing some bytes if the write buffer is not flushed correctly before the stream is closed.

--
Göran Andersson
_____
http://www.guffa.com
.


Quantcast