本文整理汇总了C#中FileStream.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.WriteAsync方法的具体用法?C# FileStream.WriteAsync怎么用?C# FileStream.WriteAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileStream
的用法示例。
在下文中一共展示了FileStream.WriteAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NegativeOffsetThrows
public void NegativeOffsetThrows()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
Assert.Throws<ArgumentOutOfRangeException>("offset", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], -1, 1)));
// buffer is checked first
Assert.Throws<ArgumentNullException>("buffer", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(null, -1, 1)));
}
}
示例2: AnonymousPipeWriteViaFileStream
public async Task AnonymousPipeWriteViaFileStream(bool asyncWrites)
{
using (var server = new AnonymousPipeServerStream(PipeDirection.In))
{
Task serverTask = Task.Run(() =>
{
for (int i = 0; i < 6; i++)
Assert.Equal(i, server.ReadByte());
});
using (var client = new FileStream(new SafeFileHandle(server.ClientSafePipeHandle.DangerousGetHandle(), false), FileAccess.Write, bufferSize: 3))
{
var data = new[] { new byte[] { 0, 1 }, new byte[] { 2, 3 }, new byte[] { 4, 5 } };
foreach (byte[] arr in data)
{
if (asyncWrites)
await client.WriteAsync(arr, 0, arr.Length);
else
client.Write(arr, 0, arr.Length);
}
}
await serverTask;
}
}
示例3: NullBufferThrows
public void NullBufferThrows()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
Assert.Throws<ArgumentNullException>("buffer", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(null, 0, 1)));
}
}
示例4: BufferOutOfBoundsThrows
public void BufferOutOfBoundsThrows()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
// offset out of bounds
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 1, 1)));
// offset out of bounds for 0 count WriteAsync
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 2, 0)));
// offset out of bounds even for 0 length buffer
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[0], 1, 0)));
// combination offset and count out of bounds
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[2], 1, 2)));
// edges
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[0], int.MaxValue, 0)));
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[0], int.MaxValue, int.MaxValue)));
}
}
示例5: WriteAsyncCore
private static async Task WriteAsyncCore(FileStream writer, int innerIterations, byte[] bytes)
{
for (int i = 0; i < innerIterations; i++)
await writer.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
}
示例6: WriteAsyncDisposedThrows
public void WriteAsyncDisposedThrows()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
fs.Dispose();
Assert.Throws<ObjectDisposedException>(() =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, 1)));
// even for noop WriteAsync
Assert.Throws<ObjectDisposedException>(() =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, 0)));
// out of bounds checking happens first
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[2], 1, 2)));
// count is checked prior
Assert.Throws<ArgumentOutOfRangeException>("count", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, -1)));
// offset is checked prior
Assert.Throws<ArgumentOutOfRangeException>("offset", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], -1, -1)));
// buffer is checked first
Assert.Throws<ArgumentNullException>("buffer", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(null, -1, -1)));
}
}
示例7: WriteAsyncMiniStress
public async Task WriteAsyncMiniStress()
{
TimeSpan testRunTime = TimeSpan.FromSeconds(10);
const int MaximumWriteSize = 16 * 1024;
const int NormalWriteSize = 4 * 1024;
Random rand = new Random();
DateTime testStartTime = DateTime.UtcNow;
// Generate data to write (NOTE: Randomizing this is important as some file systems may optimize writing 0s)
byte[] dataToWrite = new byte[MaximumWriteSize];
rand.NextBytes(dataToWrite);
string writeFileName = GetTestFilePath();
do
{
// Create a new token that expires between 100-1000ms
CancellationTokenSource tokenSource = new CancellationTokenSource();
tokenSource.CancelAfter(rand.Next(100, 1000));
using (var stream = new FileStream(writeFileName, FileMode.Create, FileAccess.Write))
{
do
{
try
{
// 20%: random write size
int bytesToWrite = (rand.NextDouble() < 0.2 ? rand.Next(16, MaximumWriteSize) : NormalWriteSize);
if (rand.NextDouble() < 0.1)
{
// 10%: Sync write
stream.Write(dataToWrite, 0, bytesToWrite);
}
else
{
// 90%: Async write
await stream.WriteAsync(dataToWrite, 0, bytesToWrite, tokenSource.Token);
}
}
catch (TaskCanceledException)
{
Assert.True(tokenSource.Token.IsCancellationRequested, "Received cancellation exception before token expired");
}
} while (!tokenSource.Token.IsCancellationRequested);
}
} while (DateTime.UtcNow - testStartTime <= testRunTime);
}
示例8: BufferCorrectlyMaintainedWhenReadAndWrite
public async Task BufferCorrectlyMaintainedWhenReadAndWrite(bool useAsync)
{
string path = GetTestFilePath();
File.WriteAllBytes(path, TestBuffer);
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 2, useAsync))
{
Assert.Equal(TestBuffer[0], await ReadByteAsync(fs));
Assert.Equal(TestBuffer[1], await ReadByteAsync(fs));
Assert.Equal(TestBuffer[2], await ReadByteAsync(fs));
await fs.WriteAsync(TestBuffer, 0, TestBuffer.Length);
fs.Position = 0;
Assert.Equal(TestBuffer[0], await ReadByteAsync(fs));
Assert.Equal(TestBuffer[1], await ReadByteAsync(fs));
Assert.Equal(TestBuffer[2], await ReadByteAsync(fs));
for (int i = 0; i < TestBuffer.Length; i++)
{
Assert.Equal(TestBuffer[i], await ReadByteAsync(fs));
}
}
}
示例9: ManyConcurrentWriteAsyncs
[OuterLoop] // many combinations: we test just one in inner loop and the rest outer
public async Task ManyConcurrentWriteAsyncs(
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);
CancellationToken cancellationToken = cancelable ? new CancellationTokenSource().Token : CancellationToken.None;
string path = GetTestFilePath();
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, useAsync))
{
if (presize)
{
fs.SetLength(totalLength);
}
if (exposeHandle)
{
var ignored = fs.SafeFileHandle;
}
Task[] writes = new Task[numWrites];
for (int i = 0; i < numWrites; i++)
{
writes[i] = fs.WriteAsync(expectedData, i * writeSize, writeSize, cancellationToken);
Assert.Null(writes[i].Exception);
if (useAsync)
{
Assert.Equal((i + 1) * writeSize, fs.Position);
}
}
await Task.WhenAll(writes);
}
byte[] actualData = File.ReadAllBytes(path);
Assert.Equal(expectedData.Length, actualData.Length);
Assert.Equal<byte>(expectedData, actualData);
}
示例10: ReadOnlyThrows
public void ReadOnlyThrows()
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{ }
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
Assert.Throws<NotSupportedException>(() =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, 1)));
fs.Dispose();
// Disposed checking happens first
Assert.Throws<ObjectDisposedException>(() =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, 1)));
// out of bounds checking happens first
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[2], 1, 2)));
// count is checked prior
Assert.Throws<ArgumentOutOfRangeException>("count", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, -1)));
// offset is checked prior
Assert.Throws<ArgumentOutOfRangeException>("offset", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], -1, -1)));
// buffer is checked first
Assert.Throws<ArgumentNullException>("buffer", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(null, -1, -1)));
}
}
示例11: WriteAsyncCancelledFile
public async Task WriteAsyncCancelledFile()
{
const int writeSize = 1024 * 1024;
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
byte[] buffer = new byte[writeSize];
CancellationTokenSource cts = new CancellationTokenSource();
Task writeTask = fs.WriteAsync(buffer, 0, buffer.Length, cts.Token);
cts.Cancel();
try
{
await writeTask;
}
catch (OperationCanceledException oce)
{
// Ideally we'd be doing an Assert.Throws<OperationCanceledException>
// but since cancellation is a race condition we accept either outcome
Assert.Equal(cts.Token, oce.CancellationToken);
}
}
}
示例12: SimpleWriteAsync
public async Task SimpleWriteAsync()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
await fs.WriteAsync(TestBuffer, 0, TestBuffer.Length);
Assert.Equal(TestBuffer.Length, fs.Length);
Assert.Equal(TestBuffer.Length, fs.Position);
fs.Position = 0;
byte[] buffer = new byte[TestBuffer.Length];
Assert.Equal(TestBuffer.Length, await fs.ReadAsync(buffer, 0, buffer.Length));
Assert.Equal(TestBuffer, buffer);
}
}
示例13: WriteAsyncBufferedCompletesSynchronously
public void WriteAsyncBufferedCompletesSynchronously()
{
using (FileStream fs = new FileStream(
GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete,
TestBuffer.Length * 2, useAsync: true))
{
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[TestBuffer.Length], 0, TestBuffer.Length));
}
}
示例14: NoopWriteAsyncsSucceed
public async Task NoopWriteAsyncsSucceed()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
// note that these do not succeed synchronously even though they do nothing.
await fs.WriteAsync(new byte[0], 0, 0);
await fs.WriteAsync(new byte[1], 0, 0);
// even though offset is out of bounds of buffer, this is still allowed
// for the last element
await fs.WriteAsync(new byte[1], 1, 0);
await fs.WriteAsync(new byte[2], 1, 0);
Assert.Equal(0, fs.Length);
Assert.Equal(0, fs.Position);
}
}
示例15: CancelledTokenFastPath
public void CancelledTokenFastPath()
{
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
CancellationToken cancelledToken = cts.Token;
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
FSAssert.IsCancelled(fs.WriteAsync(new byte[1], 0, 1, cancelledToken), cancelledToken);
}
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
// before read only check
FSAssert.IsCancelled(fs.WriteAsync(new byte[1], 0, 1, cancelledToken), cancelledToken);
fs.Dispose();
// before disposed check
FSAssert.IsCancelled(fs.WriteAsync(new byte[1], 0, 1, cancelledToken), cancelledToken);
// out of bounds checking happens first
Assert.Throws<ArgumentException>(null, () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[2], 1, 2, cancelledToken)));
// count is checked prior
Assert.Throws<ArgumentOutOfRangeException>("count", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], 0, -1, cancelledToken)));
// offset is checked prior
Assert.Throws<ArgumentOutOfRangeException>("offset", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(new byte[1], -1, -1, cancelledToken)));
// buffer is checked first
Assert.Throws<ArgumentNullException>("buffer", () =>
FSAssert.CompletesSynchronously(fs.WriteAsync(null, -1, -1, cancelledToken)));
}
}