Output stream deleting last few bytes

M

Mike P2

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)

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mike said:
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.
 
M

Mike P2

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.

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.

Thanks, that was it!

I added the other arguments into GetString, and my problem was that I
was not overriding Close() (I had overridden Flush()).

Here's what I was doing with Flush(), I just did the same thing for
Close() but calling Close() on _origStream:

Public Overrides Sub Flush()
_origStream.Flush()
End Sub

Problem solved

-Mike PII
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top