本文整理汇总了C#中FileStream.CopyToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.CopyToAsync方法的具体用法?C# FileStream.CopyToAsync怎么用?C# FileStream.CopyToAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileStream
的用法示例。
在下文中一共展示了FileStream.CopyToAsync方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InvalidArgs_Throws
public void InvalidArgs_Throws(bool useAsync)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
Assert.Throws<ArgumentNullException>("destination", () => { fs.CopyToAsync(null); });
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => { fs.CopyToAsync(new MemoryStream(), 0); });
Assert.Throws<NotSupportedException>(() => { fs.CopyToAsync(new MemoryStream(new byte[1], writable: false)); });
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
fs.SafeFileHandle.Dispose();
Assert.Throws<ObjectDisposedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream src = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
using (FileStream dst = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
dst.Dispose();
Assert.Throws<ObjectDisposedException>(() => { src.CopyToAsync(dst); });
}
}
示例2: AlreadyCanceled_ReturnsCanceledTask
public async Task AlreadyCanceled_ReturnsCanceledTask(bool useAsync)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 0x100, useAsync))
{
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => fs.CopyToAsync(fs, 0x1000, new CancellationToken(canceled: true)));
}
}
示例3: File_AllDataCopied
public async Task File_AllDataCopied(
Func<string, Stream> createDestinationStream,
bool useAsync, bool preRead, bool preWrite, bool exposeHandle, bool cancelable,
int bufferSize, int writeSize, int numWrites)
{
// Create the expected data
long totalLength = writeSize * numWrites;
var expectedData = new byte[totalLength];
new Random(42).NextBytes(expectedData);
// Write it out into the source file
string srcPath = GetTestFilePath();
File.WriteAllBytes(srcPath, expectedData);
string dstPath = GetTestFilePath();
using (FileStream src = new FileStream(srcPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None, bufferSize, useAsync))
using (Stream dst = createDestinationStream(dstPath))
{
// If configured to expose the handle, do so. This influences the stream's need to ensure the position is in sync.
if (exposeHandle)
{
var ignored = src.SafeFileHandle;
}
// If configured to "preWrite", do a write before we start reading.
if (preWrite)
{
src.Write(new byte[] { 42 }, 0, 1);
dst.Write(new byte[] { 42 }, 0, 1);
expectedData[0] = 42;
}
// If configured to "preRead", read one byte from the source prior to the CopyToAsync.
// This helps test what happens when there's already data in the buffer, when the position
// isn't starting at zero, etc.
if (preRead)
{
int initialByte = src.ReadByte();
if (initialByte >= 0)
{
dst.WriteByte((byte)initialByte);
}
}
// Do the copy
await src.CopyToAsync(dst, writeSize, cancelable ? new CancellationTokenSource().Token : CancellationToken.None);
dst.Flush();
// Make sure we're at the end of the source file
Assert.Equal(src.Length, src.Position);
// Verify the copied data
dst.Position = 0;
var result = new MemoryStream();
dst.CopyTo(result);
byte[] actualData = result.ToArray();
Assert.Equal(expectedData.Length, actualData.Length);
Assert.Equal<byte>(expectedData, actualData);
}
}
示例4: NamedPipeViaFileStream_CancellationRequested_OperationCanceled
public async Task NamedPipeViaFileStream_CancellationRequested_OperationCanceled()
{
string name = Guid.NewGuid().ToString("N");
using (var server = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
Task serverTask = server.WaitForConnectionAsync();
Assert.True(WaitNamedPipeW(@"\\.\pipe\" + name, -1));
using (SafeFileHandle clientHandle = CreateFileW(@"\\.\pipe\" + name, GENERIC_READ, FileShare.None, IntPtr.Zero, FileMode.Open, (int)PipeOptions.Asynchronous, IntPtr.Zero))
using (var client = new FileStream(clientHandle, FileAccess.Read, bufferSize: 3, isAsync: true))
{
await serverTask;
var cts = new CancellationTokenSource();
Task clientTask = client.CopyToAsync(new MemoryStream(), 0x1000, cts.Token);
Assert.False(clientTask.IsCompleted);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(() => clientTask);
}
}
}
示例5: NamedPipeViaFileStream_AllDataCopied
public async Task NamedPipeViaFileStream_AllDataCopied(bool useAsync, int writeSize, int numWrites)
{
long totalLength = writeSize * numWrites;
var expectedData = new byte[totalLength];
new Random(42).NextBytes(expectedData);
var results = new MemoryStream();
var pipeOptions = useAsync ? PipeOptions.Asynchronous : PipeOptions.None;
string name = Guid.NewGuid().ToString("N");
using (var server = new NamedPipeServerStream(name, PipeDirection.Out, 1, PipeTransmissionMode.Byte, pipeOptions))
{
Task serverTask = Task.Run(async () =>
{
await server.WaitForConnectionAsync();
for (int i = 0; i < numWrites; i++)
{
await server.WriteAsync(expectedData, i * writeSize, writeSize);
}
server.Dispose();
});
Assert.True(WaitNamedPipeW(@"\\.\pipe\" + name, -1));
using (SafeFileHandle clientHandle = CreateFileW(@"\\.\pipe\" + name, GENERIC_READ, FileShare.None, IntPtr.Zero, FileMode.Open, (int)pipeOptions, IntPtr.Zero))
using (var client = new FileStream(clientHandle, FileAccess.Read, bufferSize: 3, isAsync: useAsync))
{
Task copyTask = client.CopyToAsync(results, (int)totalLength);
await await Task.WhenAny(serverTask, copyTask);
await copyTask;
}
}
byte[] actualData = results.ToArray();
Assert.Equal(expectedData.Length, actualData.Length);
Assert.Equal<byte>(expectedData, actualData);
}
示例6: AnonymousPipeViaFileStream_AllDataCopied
public async Task AnonymousPipeViaFileStream_AllDataCopied(int writeSize, int numWrites)
{
long totalLength = writeSize * numWrites;
var expectedData = new byte[totalLength];
new Random(42).NextBytes(expectedData);
var results = new MemoryStream();
using (var server = new AnonymousPipeServerStream(PipeDirection.Out))
{
Task serverTask = Task.Run(async () =>
{
for (int i = 0; i < numWrites; i++)
{
await server.WriteAsync(expectedData, i * writeSize, writeSize);
}
});
using (var client = new FileStream(new SafeFileHandle(server.ClientSafePipeHandle.DangerousGetHandle(), false), FileAccess.Read, bufferSize: 3))
{
Task copyTask = client.CopyToAsync(results, writeSize);
await await Task.WhenAny(serverTask, copyTask);
server.Dispose();
await copyTask;
}
}
byte[] actualData = results.ToArray();
Assert.Equal(expectedData.Length, actualData.Length);
Assert.Equal<byte>(expectedData, actualData);
}
示例7: CopyToAsyncBetweenFileStreams
[OuterLoop] // many combinations: we test just one in inner loop and the rest outer
public async Task CopyToAsyncBetweenFileStreams(
bool useAsync, bool preSize, bool exposeHandle, bool cancelable, int bufferSize, int writeSize, int numWrites)
{
long totalLength = writeSize * numWrites;
var expectedData = new byte[totalLength];
new Random(42).NextBytes(expectedData);
string srcPath = GetTestFilePath();
File.WriteAllBytes(srcPath, expectedData);
string dstPath = GetTestFilePath();
using (FileStream src = new FileStream(srcPath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, useAsync))
using (FileStream dst = new FileStream(dstPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, useAsync))
{
await src.CopyToAsync(dst, writeSize, cancelable ? new CancellationTokenSource().Token : CancellationToken.None);
}
byte[] actualData = File.ReadAllBytes(dstPath);
Assert.Equal(expectedData.Length, actualData.Length);
Assert.Equal<byte>(expectedData, actualData);
}
示例8: CopyToAsync_InvalidArgs_Throws
public void CopyToAsync_InvalidArgs_Throws()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
Assert.Throws<ArgumentNullException>("destination", () => { fs.CopyToAsync(null); });
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => { fs.CopyToAsync(new MemoryStream(), 0); });
Assert.Throws<NotSupportedException>(() => { fs.CopyToAsync(new MemoryStream(new byte[1], writable: false)); });
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => { fs.CopyToAsync(new MemoryStream()); });
}
}