This project is read-only.

InternalFileDownload output Stream is not properly closed?

Feb 23, 2012 at 1:09 PM

if the download is completed it seems that the output Stream (Destination Stream) still opened.

I guess to correct this, just need to put one more Line to the Code!

private void InternalFileDownload(ChannelSession channel, Stream input, Stream output, string filename, long length)
            var buffer = new byte[Math.Min(length, this.BufferSize)];
            var needToRead = length;

                var read = input.Read(buffer, 0, (int)Math.Min(needToRead, this.BufferSize));

                output.Write(buffer, 0, read);

                this.RaiseDownloadingEvent(filename, length, length - needToRead);

                needToRead -= read;
            while (needToRead > 0);



            //  Raise one more time when file downloaded
            this.RaiseDownloadingEvent(filename, length, length - needToRead);

            //  Send confirmation byte after last data byte was read


I wrote a test routine that downlaod over and over the same files... (112 of the same files). After the first loop an IO Exception pop out of the debugger. If I close the Stream, it appear to clean up the read/write rights for the file, else it continue to stay locked! Maybe it better to make a dispose on the stream itself!

Feb 23, 2012 at 1:40 PM



Actually its not a good practice to close or dispose of a stream that you created.

Since stream is passed as a parameter, its up to the caller to create and dispose of the stream.


Microsoft actually have bug in one of the functions where they except stream as a parameter then internally dispose it and then when caller tries to dispose it again, it throws an exception that object been already disposed.