473,414 Members | 1,605 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,414 software developers and data experts.

Please explain DiscardBufferedData

Hi,

Can someone please explain to me what the
StreamReader.DiscardBufferedData method does?
The documentation says
"Use DiscardBufferedData to seek to a known location in the underlying
stream and then begin reading from this new point, or to read the
contents
of a StreamReader more than once."
I am not able to understand what exactly this means.

I am particularly looking for how this method manipulates the buffer.

In our VB .NET program, we spawn an FTP process to which we write
commands and read back the result. For this we use StandardInput and
StandardOutput property of the Process.

To send a command to the process, we do:
SendCommand:
Me.FTPProcess.StandardInput.WriteLine(cmd)
Me.FTPProcess.StandardOutput.DiscardBufferedData()

To read the result of the command we have:
ReadResult:
While Me.FTPProcess.StandardOutput.Peek() > -1
ftpResp = Me.FTPProcess.StandardOutput.ReadLine()
End While

When sending the command, if we do not use the DiscardBufferedData, we
noticed that the program does not read the result. The Peek function
returns -1. For some commands, the peek method also blocked
indefinitely.
If the DiscardBufferedData is kept, output from previous commands is
read followed by output of the recent command. This is probably what
is meant by the above line from the documentation.

We want to ensure that after every command, only the output of that
command is read.

Any ideas or suggestions?

Thanks
Jul 21 '05 #1
1 4472
Yash <ya****@yahoo.com> wrote:
Can someone please explain to me what the
StreamReader.DiscardBufferedData method does?
Sure. When you ask a StreamReader to read some data, it reads more than
you ask for, for performance reasons. Next time you ask it to read, if
it can satisfy the request without reading anything else from the
underlying stream, it will do so. That causes problems if you're
repositioning the underlying stream, or something like that.
In our VB .NET program, we spawn an FTP process to which we write
commands and read back the result. For this we use StandardInput and
StandardOutput property of the Process.

To send a command to the process, we do:
SendCommand:
Me.FTPProcess.StandardInput.WriteLine(cmd)
Me.FTPProcess.StandardOutput.DiscardBufferedData()
I see no reason to call DiscardBufferedData there.
To read the result of the command we have:
ReadResult:
While Me.FTPProcess.StandardOutput.Peek() > -1
ftpResp = Me.FTPProcess.StandardOutput.ReadLine()
End While

When sending the command, if we do not use the DiscardBufferedData, we
noticed that the program does not read the result. The Peek function
returns -1. For some commands, the peek method also blocked
indefinitely.
If the DiscardBufferedData is kept, output from previous commands is
read followed by output of the recent command. This is probably what
is meant by the above line from the documentation.

We want to ensure that after every command, only the output of that
command is read.

Any ideas or suggestions?


I think your manner of reading results is basically somewhat flawed. It
will think that a command's result has completed even if it's really
just pausing, for instance.

Instead, you should try to work out what the process will return as the
final line when it's finished, and loop until you see that (or the
stream is closed).

Having said all that, it would probably be easier to use an FTP library
instead of spawning a process in the first place.

See http://www.indyproject.org for one such library.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

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

Similar topics

9
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
3
by: VijayShankar | last post by:
Can u be more specific on your question Anyway its not like Session variables are available for sometime and not available for sometime. When your session starts it is very much available...
12
by: Sanjeev | last post by:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? //////////////////////////////////////////////// #include<stdio.h> #include<string.h> void main() {
1
by: Yash | last post by:
Hi, Can someone please explain to me what the StreamReader.DiscardBufferedData method does? The documentation says "Use DiscardBufferedData to seek to a known location in the underlying stream...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
9
by: colin.mcnulty | last post by:
Hi, I'm a SQL Server DBA, but I guess that won't buy me any friends round here huh? ;-) I've been asked to look at the SQL that's being executed on a DB2 database from a web app, specifically...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
2
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'}; printf("strlen=%d\n", strlen(a));...
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
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...
0
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...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.