473,769 Members | 5,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Enc oding.UTF8.GetS tring(stuff)

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

'convert to array of bytes again and give it back
stuff = System.Text.Enc oding.UTF8.GetB ytes(outgoing)
_origStream.Wri te(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 1479
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.Enc oding.UTF8.GetS tring(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(o utgoing, "\n\s+", ControlChars.Lf )
'this one is only useful for the first chunk:
outgoing = Regex.Replace(o utgoing, "^\s+<!DOC" , "<!DOC")

'convert to array of bytes again and give it back
stuff = System.Text.Enc oding.UTF8.GetB ytes(outgoing)
_origStream.Wri te(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.com wrote:
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.Flu sh()
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
2717
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 Contest is scheduled on Sunday, 27 Feb 2005. This is the first Online Programming Contest in India to support Python !!!!. Other languages supported are C and C++.
5
5197
by: Michael | last post by:
I mean how to use _tprintf().
6
1589
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 should depend on user input (command line arguments actually). If the user decides he/she wants verbose output, I will pass std::cout as the last argument when calling foo(). But what should I pass if the user doesn't want any output? And how...
5
8057
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, and need to manage all socket traffic. My thought was a stateful call to my own Encrypt and Decrypt routines. That I would pass a MemoryStream to the CryptoStream constructor. But it appears that for each write CryptoStream does to my memory...
10
9668
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
6774
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 on the stream. What am I doing wrong? There is more code than this, but this is the problem code.
2
2082
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 as it is to the standard output.
3
2382
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 I turn on exceptions, the what() method from the exception that gets thrown just tells me that there's a basic_ios::clear error, and I've verified that errno is 0. Does anyone have any suggestions about what I can do?
2
1954
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. I can read the information from the stream with no problem. However, the problem comes when there is nothing to read in the stream. The stream is still open, so it just hangs on my Read(). EXA:
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3948
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.