-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefaultLanguage>en-US</DefaultLanguage>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
- Link="Common\Interop\Windows\Interop.Libraries.cs" />
+ Link="Common\Interop\Windows\Interop.Libraries.cs"
+ Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)' "/>
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
- Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs" />
+ Link="Common\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs"
+ Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)' "/>
<Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\EmptyArray.cs" />
<Compile Include="System\Reflection\Internal\Utilities\EncodingHelper.cs" Condition="$(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('net4'))" />
<Compile Include="System\Reflection\Internal\Utilities\EncodingHelper.netcoreapp.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
- <Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
+ <Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netcoreapp.cs" Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'" />
<Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
+ <Compile Include="System\Reflection\Internal\Utilities\FileStreamReadLightUp.netstandard2.0.cs" Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net461'" />
<Compile Include="System\Reflection\Internal\Utilities\Hash.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableByteArrayInterop.cs" />
<Compile Include="System\Reflection\Internal\Utilities\ImmutableMemoryStream.cs" />
{
stream.Seek(start, SeekOrigin.Begin);
- if (!isFileStream || !FileStreamReadLightUp.TryReadFile(stream, block.Pointer, start, size))
+ int bytesRead = 0;
+
+ if (!isFileStream || (bytesRead = FileStreamReadLightUp.ReadFile(stream, block.Pointer, size)) != size)
{
- stream.CopyTo(block.Pointer, size);
+ stream.CopyTo(block.Pointer + bytesRead, size - bytesRead);
}
fault = false;
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.IO;
+
+namespace System.Reflection.Internal
+{
+ internal static class FileStreamReadLightUp
+ {
+ internal static bool IsFileStream(Stream stream) => stream is FileStream;
+
+ internal static unsafe int ReadFile(Stream stream, byte* buffer, int size)
+ => stream.Read(new Span<byte>(buffer, size));
+ }
+}
return handle;
}
- internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
+ internal static unsafe int ReadFile(Stream stream, byte* buffer, int size)
{
if (readFileNotAvailable)
{
- return false;
+ return 0;
}
SafeHandle handle = GetSafeFileHandle(stream);
if (handle == null)
{
- return false;
+ return 0;
}
- int result;
- int bytesRead;
-
try
{
- result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
+ int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);
+ return result == 0 ? 0 : bytesRead;
}
catch
{
readFileNotAvailable = true;
- return false;
+ return 0;
}
-
- if (result == 0 || bytesRead != size)
- {
- // We used to throw here, but this is where we land if the FileStream was
- // opened with useAsync: true, which is currently the default on .NET Core.
- // https://github.com/dotnet/corefx/pull/987 filed to look in to how best to
- // handle this, but in the meantime, we'll fall back to the slower code path
- // just as in the case where the native API is unavailable in the current platform.
- return false;
- }
-
- return true;
}
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
{
internal static class FileStreamReadLightUp
{
- private static bool IsReadFileAvailable =>
-#if NETCOREAPP
- OperatingSystem.IsWindows();
-#else
- Path.DirectorySeparatorChar == '\\';
-#endif
+ private static bool IsReadFileAvailable => Path.DirectorySeparatorChar == '\\';
internal static bool IsFileStream(Stream stream) => stream is FileStream;
return handle;
}
- internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start, int size)
+ internal static unsafe int ReadFile(Stream stream, byte* buffer, int size)
{
if (!IsReadFileAvailable)
{
- return false;
+ return 0;
}
SafeHandle? handle = GetSafeFileHandle(stream);
if (handle == null)
{
- return false;
+ return 0;
}
int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);
-
- if (result == 0 || bytesRead != size)
- {
- // We used to throw here, but this is where we land if the FileStream was
- // opened with useAsync: true, which is currently the default on .NET Core.
- // https://github.com/dotnet/corefx/pull/987 filed to look in to how best to
- // handle this, but in the meantime, we'll fall back to the slower code path
- // just as in the case where the native API is unavailable in the current platform.
- return false;
- }
-
- return true;
+ return result == 0 ? 0 : bytesRead;
}
}
}