ReadAsync() public méthode

public ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken CancellationToken
Résultat Task
        /// <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();
            }
        }
		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;
			}
		}
Exemple #3
0
 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);
     }
 }