469,964 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

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 4254
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Sanjeev | last post: by
1 post views Thread by Yash | last post: by
61 posts views Thread by warint | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.