当前位置: 首页>>代码示例>>C#>>正文


C# BufferedStream.ReadAsync方法代码示例

本文整理汇总了C#中System.IO.BufferedStream.ReadAsync方法的典型用法代码示例。如果您正苦于以下问题:C# BufferedStream.ReadAsync方法的具体用法?C# BufferedStream.ReadAsync怎么用?C# BufferedStream.ReadAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.BufferedStream的用法示例。


在下文中一共展示了BufferedStream.ReadAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WriteResponseStreamToFileAsync

        /// <summary>
        /// Writes the content of the ResponseStream a file indicated by the filePath argument.
        /// </summary>
        /// <param name="filePath">The location where to write the ResponseStream</param>
        /// <param name="append">Whether or not to append to the file if it exists</param>
        /// <param name="cancellationToken">Cancellation token which can be used to cancel this operation.</param>
        public async Task WriteResponseStreamToFileAsync(string filePath, bool append, CancellationToken cancellationToken)
        {
            // Make sure the directory exists to write too.
            FileInfo fi = new FileInfo(filePath);
            Directory.CreateDirectory(fi.DirectoryName);
                        
            Stream downloadStream;
            if (append && File.Exists(filePath))
                downloadStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read,S3Constants.DefaultBufferSize);
            else
                downloadStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, S3Constants.DefaultBufferSize);

            try
            {
                long current = 0;
                BufferedStream bufferedStream = new BufferedStream(this.ResponseStream);
                byte[] buffer = new byte[S3Constants.DefaultBufferSize];
                int bytesRead = 0;
                long totalIncrementTransferred = 0;
                while ((bytesRead = await bufferedStream.ReadAsync(buffer, 0, buffer.Length)
                    .ConfigureAwait(continueOnCapturedContext:false)) > 0)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    await downloadStream.WriteAsync(buffer, 0, bytesRead)
                        .ConfigureAwait(continueOnCapturedContext: false);
                    current += bytesRead;
                    totalIncrementTransferred += bytesRead;

                    if (totalIncrementTransferred >= AWSSDKUtils.DefaultProgressUpdateInterval ||
                        current == this.ContentLength)
                    {
                        this.OnRaiseProgressEvent(filePath, totalIncrementTransferred, current, this.ContentLength);
                        totalIncrementTransferred = 0;
                    }
                }

                ValidateWrittenStreamSize(current);
            }
            finally
            {
                downloadStream.Close();
            }
        }
开发者ID:rajdotnet,项目名称:aws-sdk-net,代码行数:50,代码来源:GetObjectResponse.cs

示例2: ReadAllBytesAsync

		public static async Task<byte[]> ReadAllBytesAsync (string file, CancellationToken token)
		{
			using (var f = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: DefaultBufferSize, options: FileOptions.Asynchronous | FileOptions.SequentialScan))
			using (var bs = new BufferedStream (f)) {
				var res = new byte [bs.Length];
				int nr = 0;
				int c = 0;
				while (nr < res.Length && (c = await bs.ReadAsync (res, nr, res.Length - nr, token).ConfigureAwait (false)) > 0)
					nr += c;
				return res;
			}
		}
开发者ID:kdubau,项目名称:monodevelop,代码行数:12,代码来源:TextFileUtility.cs

示例3: recv

 static async Task recv(BufferedStream b, Reader r, int n)
 {
     r.Stream.Position = 0;
     int rest = n;
     while (rest > 0)
     {
         rest -= await b.ReadAsync(r.Buffer, n - rest, rest).ConfigureAwait(false);
     }
 }
开发者ID:hythof,项目名称:csharp,代码行数:9,代码来源:Program.cs


注:本文中的System.IO.BufferedStream.ReadAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。