Flush() public method

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
Once flushed, any subsequent read operations no longer block until requested bytes are available. Any write operation reactivates blocking reads.
An I/O error occurs. Methods were called after the stream was closed.
public Flush ( ) : void
return void
示例#1
0
        /// <summary>
        /// Downloads the specified directory from the remote host to local directory.
        /// </summary>
        /// <param name="directoryName">Remote host directory name.</param>
        /// <param name="directoryInfo">Local directory information.</param>
        /// <exception cref="ArgumentNullException"><paramref name="directoryInfo"/> or <paramref name="directoryName"/> is null.</exception>
        public void Download(string directoryName, DirectoryInfo directoryInfo)
        {
            if (directoryInfo == null)
                throw new ArgumentNullException("directoryInfo");

            if (string.IsNullOrEmpty(directoryName))
                throw new ArgumentException("directoryName");

            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -prf \"{0}\"", directoryName));
                this.SendConfirmation(channel); //  Send reply

                this.InternalDownload(channel, input, directoryInfo);

                channel.Close();
            }
        }
示例#2
0
        /// <summary>
        /// Downloads the specified file from the remote host to local file.
        /// </summary>
        /// <param name="filename">Remote host file name.</param>
        /// <param name="fileInfo">Local file information.</param>
        /// <exception cref="ArgumentNullException"><paramref name="fileInfo"/> or <paramref name="filename"/> is null.</exception>
        public void Download(string filename, FileInfo fileInfo)
        {
            if (fileInfo == null)
                throw new ArgumentNullException("fileInfo");

            if (filename == null)
                throw new ArgumentNullException("filename"); //  TODO:   Should add IsNullOrWhitespace for this filename parameter?

            //  UNDONE:   EnsureConnection?

            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -qpf \"{0}\"", filename));
                this.SendConfirmation(channel); //  Send reply

                this.InternalDownload(channel, input, fileInfo);

                channel.Close();
            }
        }
示例#3
0
        /// <summary>
        /// Uploads the specified directory to the remote host.
        /// </summary>
        /// <param name="directoryInfo">The directory info.</param>
        /// <param name="path">The path.</param>
        /// <exception cref="ArgumentNullException">fileSystemInfo</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> is null or empty.</exception>
        public void Upload(DirectoryInfo directoryInfo, string path)
        {
            if (directoryInfo == null)
                throw new ArgumentNullException("directoryInfo");
            if (string.IsNullOrEmpty(path))
                throw new ArgumentException("path");

            using (var input = new PipeStream())
            using (var channel = Session.CreateChannelSession())
            {
                channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -rt \"{0}\"", path));
                CheckReturnCode(input);

                InternalSetTimestamp(channel, input, directoryInfo.LastWriteTimeUtc, directoryInfo.LastAccessTimeUtc);
                SendData(channel, string.Format("D0755 0 {0}\n", Path.GetFileName(path)));
                CheckReturnCode(input);

                InternalUpload(channel, input, directoryInfo);

                SendData(channel, "E\n");
                CheckReturnCode(input);

                channel.Close();
            }
        }
示例#4
0
        /// <summary>
        /// Uploads the specified file to the remote host.
        /// </summary>
        /// <param name="fileInfo">The file system info.</param>
        /// <param name="path">The path.</param>
        /// <exception cref="ArgumentNullException"><paramref name="fileInfo" /> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> is null or empty.</exception>
        public void Upload(FileInfo fileInfo, string path)
        {
            if (fileInfo == null)
                throw new ArgumentNullException("fileInfo");
            if (string.IsNullOrEmpty(path))
                throw new ArgumentException("path");

            using (var input = new PipeStream())
            using (var channel = this.Session.CreateClientChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -t \"{0}\"", path));
                this.CheckReturnCode(input);

                this.InternalUpload(channel, input, fileInfo, fileInfo.Name);

                channel.Close();
            }
        }
示例#5
0
        /// <summary>
        /// Downloads the specified directory from the remote host to local directory.
        /// </summary>
        /// <param name="directoryName">Remote host directory name.</param>
        /// <param name="directoryInfo">Local directory information.</param>
        public void Download(string directoryName, DirectoryInfo directoryInfo)
        {
            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -qprf {0}", directoryName));
                this.SendConfirmation(channel); //  Send reply

                this.InternalDownload(channel, input, directoryInfo);

                channel.Close();
            }
        }
示例#6
0
        /// <summary>
        /// Uploads the specified file or directory to the remote host.
        /// </summary>
        /// <param name="fileInfo">Local file to upload.</param>
        /// <param name="filename">Remote host file name.</param>
        /// <exception cref="ArgumentNullException"><paramref name="fileInfo"/> or <paramref name="filename"/> is null.</exception>
        public void Upload(FileSystemInfo fileSystemInfo, string path)
        {
            if (fileSystemInfo == null)
                throw new ArgumentNullException("fileSystemInfo");

            if (string.IsNullOrEmpty(path))
                throw new ArgumentException("path");

            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                var pathParts = path.Split('\\', '/');

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -rt \"{0}\"", pathParts[0]));
                this.CheckReturnCode(input);

                //  Prepare directory structure
                for (int i = 0; i < pathParts.Length - 1; i++)
                {
                    this.InternalSetTimestamp(channel, input, fileSystemInfo.LastWriteTimeUtc, fileSystemInfo.LastAccessTimeUtc);
                    this.SendData(channel, string.Format("D0755 0 {0}\n", pathParts[i]));
                    this.CheckReturnCode(input);
                }

                if (fileSystemInfo is FileInfo)
                {
                    this.InternalUpload(channel, input, fileSystemInfo as FileInfo, pathParts.Last());
                }
                else if (fileSystemInfo is DirectoryInfo)
                {
                    this.InternalUpload(channel, input, fileSystemInfo as DirectoryInfo, pathParts.Last());
                }
                else
                {
                    throw new NotSupportedException(string.Format("Type '{0}' is not supported.", fileSystemInfo.GetType().FullName));
                }

                //  Finish directory structure
                for (int i = 0; i < pathParts.Length - 1; i++)
                {
                    this.SendData(channel, "E\n");
                    this.CheckReturnCode(input);
                }

                channel.Close();
            }
        }
示例#7
0
        /// <summary>
        /// Uploads the specified stream to the remote host.
        /// </summary>
        /// <param name="source">Stream to upload.</param>
        /// <param name="filename">Remote host file name.</param>
        public void Upload(Stream source, string path)
        {
            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                var pathParts = path.Split('\\', '/');

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -rt \"{0}\"", pathParts[0]));
                this.CheckReturnCode(input);

                //  Prepare directory structure
                for (int i = 0; i < pathParts.Length - 1; i++)
                {
                    this.InternalSetTimestamp(channel, input, DateTime.UtcNow, DateTime.UtcNow);
                    this.SendData(channel, string.Format("D0755 0 {0}\n", pathParts[i]));
                    this.CheckReturnCode(input);
                }

                this.InternalUpload(channel, input, source, pathParts.Last());

                //  Finish directory structure
                for (int i = 0; i < pathParts.Length - 1; i++)
                {
                    this.SendData(channel, "E\n");
                    this.CheckReturnCode(input);
                }

                channel.Close();
            }
        }
示例#8
0
        /// <summary>
        /// Downloads the specified file from the remote host to the stream.
        /// </summary>
        /// <param name="filename">Remote host file name.</param>
        /// <param name="destination">The stream where to download remote file.</param>
        /// <exception cref="ArgumentException"><paramref name="filename"/> is null or contains whitespace characters.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="destination"/> is null.</exception>
        /// <remarks>Method calls made by this method to <paramref name="destination"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
        public void Download(string filename, Stream destination)
        {
            if (filename.IsNullOrWhiteSpace())
                throw new ArgumentException("filename");

            if (destination == null)
                throw new ArgumentNullException("destination");

            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -f \"{0}\"", filename));
                this.SendConfirmation(channel); //  Send reply

                var message = ReadString(input);
                var match = _fileInfoRe.Match(message);

                if (match.Success)
                {
                    //  Read file
                    this.SendConfirmation(channel); //  Send reply

                    var mode = match.Result("${mode}");
                    var length = long.Parse(match.Result("${length}"));
                    var fileName = match.Result("${filename}");

                    this.InternalDownload(channel, input, destination, fileName, length);
                }
                else
                {
                    this.SendConfirmation(channel, 1, string.Format("\"{0}\" is not valid protocol message.", message));
                }

                channel.Close();
            }
        }
示例#9
0
        /// <summary>
        /// Uploads the specified stream to the remote host.
        /// </summary>
        /// <param name="source">Stream to upload.</param>
        /// <param name="filename">Remote host file name.</param>
        public void Upload(Stream source, string filename)
        {
            using (var input = new PipeStream())
            using (var channel = this.Session.CreateChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, Common.ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                //  Send channel command request
                channel.SendExecRequest(string.Format("scp -qt \"{0}\"", filename));
                this.CheckReturnCode(input);

                this.InternalFileUpload(channel, input, source, filename);

                channel.Close();
            }
        }
示例#10
0
 public void FlushTest()
 {
     PipeStream target = new PipeStream(); // TODO: Initialize to an appropriate value
     target.Flush();
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
示例#11
0
        /// <summary>
        /// Uploads the specified stream to the remote host.
        /// </summary>
        /// <param name="source">Stream to upload.</param>
        /// <param name="path">Remote host file name.</param>
        public void Upload(Stream source, string path)
        {
            using (var input = new PipeStream())
            using (var channel = this.Session.CreateClientChannel<ChannelSession>())
            {
                channel.DataReceived += delegate(object sender, ChannelDataEventArgs e)
                {
                    input.Write(e.Data, 0, e.Data.Length);
                    input.Flush();
                };

                channel.Open();

                int pathEnd = path.LastIndexOfAny(new[] { '\\', '/' });
                if (pathEnd != -1)
                {
                    // split the path from the file
                    string pathOnly = path.Substring(0, pathEnd);
                    string fileOnly = path.Substring(pathEnd + 1);
                    //  Send channel command request
                    channel.SendExecRequest(string.Format("scp -t \"{0}\"", pathOnly));
                    this.CheckReturnCode(input);

                    path = fileOnly;
                }

                this.InternalUpload(channel, input, source, path);

                channel.Close();
            }
        }