Beispiel #1
0
        public async Task ReadAsync(IStorageFileInfo fileInfo, Stream writeStream, CancellationToken cancellationToken = default)
        {
            fileInfo.NotNull(nameof(fileInfo));
            writeStream.NotNull(nameof(writeStream));

            var buffer = new byte[Options.BufferSize];

            if (writeStream.CanSeek)
            {
                writeStream.Seek(0, SeekOrigin.Begin);
            }

            using (var readStream = fileInfo.CreateReadStream())
            {
                var currentCount = 1;
                while (currentCount > 0)
                {
                    // 每次从文件流中读取指定缓冲区的字节数,当读完后退出循环
                    currentCount = await readStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait();

                    // 将读取到的缓冲区字节数写入请求流
                    await writeStream.WriteAsync(buffer, 0, currentCount, cancellationToken).ConfigureAwait();

                    if (ProgressAction.IsNotNull())
                    {
                        var descriptor = new StorageProgressDescriptor(
                            readStream.Length, readStream.Position,
                            writeStream.Length, writeStream.Position, currentCount);

                        ProgressAction.Invoke(descriptor);
                    }
                }
            }
        }
Beispiel #2
0
        public Task <string> ReadStringAsync(IStorageFileInfo fileInfo)
        {
            fileInfo.NotNull(nameof(fileInfo));

            using (var readStream = fileInfo.CreateReadStream())
                using (var sr = new StreamReader(readStream))
                {
                    return(sr.ReadToEndAsync());
                }
        }
Beispiel #3
0
        public Task WriteStringAsync(IStorageFileInfo fileInfo, string content)
        {
            fileInfo.NotNull(nameof(fileInfo));
            content.NotEmpty(nameof(content));

            using (var readStream = fileInfo.CreateReadStream())
                using (var sw = new StreamWriter(readStream))
                {
                    return(sw.WriteAsync(content));
                }
        }