本文整理汇总了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();
}
}
示例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;
}
}
示例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);
}
}