473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Output stream deleting last few bytes

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

May 28 '07 #1
2 1463
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
May 29 '07 #2
On May 29, 1:44 pm, Göran Andersson <g...@guffa.comwrote:
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.

--
Göran Andersson
_____http://www.guffa.com
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

May 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Varun | last post by:
Hi Friends, Department of Information Technology, Madras Institute of Technology, Anna University, India is conducting a technical symposium, Samhita. As a part of samhita, an Online Programming...
5
by: Michael | last post by:
I mean how to use _tprintf().
6
by: William Payne | last post by:
Hi, I have a function declared as: void foo(const std::string& s, std::ostream& verbose_output); I want foo() to write a lot of data to the ostream if it's a valid stream. If it's valid or not...
5
by: Lee Gillie | last post by:
I am using Cryptography. You can encrypt or decrypt by providing an output stream as a parameter to the CryptoStream constructor. But I need byte arrays, as I am encrypting on the fly to a socket,...
10
by: Johannes Barop | last post by:
Hi, I want to format the output of a 'std::ostream', but i dont know how to do it . Example: int main() { my_out << "Hi.\nI'am a" << " Computer."; my_out << "Nice.";
3
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk...
2
by: arnuld | last post by:
I have 2 programs. 1st PROGRAM: copies an input file to standard output but it does so at the expense of deleting all white-space (space, tabs, newlinies etc). 2nd PROGRAM: copies the input...
3
by: dont_spam_me | last post by:
I'm running an application on an embedded device running Linux (2.6.21 kernel) . I've found that when the CPU load gets really high on the device, output to a file stream sometimes fails. When...
2
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
I've made a post last Friday in regards to this subject, but I'm still a little lost. I've a problem regarding reading a stream. I am connected to a port that sends information using a stream. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.