Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,6 @@
<data name="Arg_GuidArrayCtor" xml:space="preserve">
<value>Byte array for Guid must be exactly {0} bytes long.</value>
</data>
<data name="Arg_HandleNotAsync" xml:space="preserve">
<value>Handle does not support asynchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened synchronously (that is, it was not opened for overlapped I/O).</value>
</data>
<data name="Arg_HandleNotSync" xml:space="preserve">
<value>Handle does not support synchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened asynchronously (that is, it was opened explicitly for overlapped I/O).</value>
</data>
<data name="Arg_HexBinaryStylesNotSupported" xml:space="preserve">
<value>The number styles AllowHexSpecifier and AllowBinarySpecifier are not supported on floating point data types.</value>
</data>
Expand Down
22 changes: 4 additions & 18 deletions src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public FileStream(IntPtr handle, FileAccess access, bool ownsHandle, int bufferS
SafeFileHandle safeHandle = new SafeFileHandle(handle, ownsHandle: ownsHandle);
try
{
ValidateHandle(safeHandle, access, bufferSize, isAsync);
ValidateHandle(safeHandle, access, bufferSize);

_strategy = FileStreamHelpers.ChooseStrategy(this, safeHandle, access, bufferSize, isAsync);
_strategy = FileStreamHelpers.ChooseStrategy(this, safeHandle, access, bufferSize, safeHandle.IsAsync);
}
catch
{
Expand Down Expand Up @@ -84,20 +84,6 @@ private static void ValidateHandle(SafeFileHandle handle, FileAccess access, int
}
}

private static void ValidateHandle(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
{
ValidateHandle(handle, access, bufferSize);

if (isAsync && !handle.IsAsync)
{
ThrowHelper.ThrowArgumentException_HandleNotAsync(nameof(handle));
}
else if (!isAsync && handle.IsAsync)
{
ThrowHelper.ThrowArgumentException_HandleNotSync(nameof(handle));
}
}

public FileStream(SafeFileHandle handle, FileAccess access)
: this(handle, access, DefaultBufferSize)
{
Expand All @@ -112,9 +98,9 @@ public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize)

public FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync)
{
ValidateHandle(handle, access, bufferSize, isAsync);
ValidateHandle(handle, access, bufferSize);

_strategy = FileStreamHelpers.ChooseStrategy(this, handle, access, bufferSize, isAsync);
_strategy = FileStreamHelpers.ChooseStrategy(this, handle, access, bufferSize, handle.IsAsync);
}

public FileStream(string path, FileMode mode)
Expand Down
12 changes: 0 additions & 12 deletions src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,6 @@ internal static void ThrowArgumentException(ExceptionResource resource, Exceptio
throw GetArgumentException(resource, argument);
}

[DoesNotReturn]
internal static void ThrowArgumentException_HandleNotSync(string paramName)
{
throw new ArgumentException(SR.Arg_HandleNotSync, paramName);
}

[DoesNotReturn]
internal static void ThrowArgumentException_HandleNotAsync(string paramName)
{
throw new ArgumentException(SR.Arg_HandleNotAsync, paramName);
}

[DoesNotReturn]
internal static void ThrowArgumentNullException(ExceptionArgument argument)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
using Xunit;

Expand Down Expand Up @@ -28,17 +29,31 @@ public void MatchedAsync()
}
}

[Fact]
public void UnmatchedAsyncThrows()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task UnmatchedAsyncIsAllowed(bool isAsync)
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 4096, true))
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 4096, isAsync))
{
Assert.Throws<ArgumentException>(() => CreateFileStream(fs.SafeFileHandle, FileAccess.ReadWrite, 4096, false));
}
// isAsync parameter is now ignored, handle.IsAsync is used instead
using (FileStream newFs = CreateFileStream(fs.SafeFileHandle, FileAccess.ReadWrite, 4096, !isAsync))
{
// Verify that the new FileStream uses handle's IsAsync, not the parameter
Assert.Equal(isAsync, newFs.IsAsync);

using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 4096, false))
{
Assert.Throws<ArgumentException>(() => CreateFileStream(fs.SafeFileHandle, FileAccess.ReadWrite, 4096, true));
// Perform async write, seek to beginning, and async read to verify functionality
byte[] writeBuffer = new byte[] { 1, 2, 3, 4, 5 };
await newFs.WriteAsync(writeBuffer, 0, writeBuffer.Length);

newFs.Seek(0, SeekOrigin.Begin);

byte[] readBuffer = new byte[writeBuffer.Length];
int bytesRead = await newFs.ReadAsync(readBuffer, 0, readBuffer.Length);

Assert.Equal(writeBuffer.Length, bytesRead);
Assert.Equal(writeBuffer, readBuffer);
}
}
}
}
Expand Down
Loading