[SkipOnPlatform(TestPlatforms.Browser,"lots of operations aren't supported on browser")]// copied from StreamConformanceTests base class due to https://github.com/xunit/xunit/issues/2186
[SkipOnPlatform(TestPlatforms.Browser,"lots of operations aren't supported on browser")]// copied from StreamConformanceTests base class due to https://github.com/xunit/xunit/issues/2186
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
usingSystem.Buffers;
usingSystem.Diagnostics;
usingMicrosoft.Win32.SafeHandles;
namespaceSystem.IO
{
publicstaticpartialclassFile
{
/// <summary>
/// Initializes a new instance of the <see cref="System.IO.FileStream" /> class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, additional file options and the allocation size.
/// Initializes a new instance of the <see cref="FileStream" /> class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, additional file options and the allocation size.
/// </summary>
/// <remarks><see cref="System.IO.FileStream(string,System.IO.FileStreamOptions)"/> for information about exceptions.</remarks>
/// <remarks><see cref="FileStream(string,System.IO.FileStreamOptions)"/> for information about exceptions.</remarks>
/// Initializes a new instance of the <see cref="Microsoft.Win32.SafeHandles.SafeFileHandle" /> class with the specified path, creation mode, read/write and sharing permission, the access other SafeFileHandles can have to the same file, additional file options and the allocation size.
/// </summary>
/// <param name="path">A relative or absolute path for the file that the current <see cref="Microsoft.Win32.SafeHandles.SafeFileHandle" /> instance will encapsulate.</param>
/// <param name="mode">One of the enumeration values that determines how to open or create the file. The default value is <see cref="FileMode.Open" /></param>
/// <param name="access">A bitwise combination of the enumeration values that determines how the file can be accessed. The default value is <see cref="FileAccess.Read" /></param>
/// <param name="share">A bitwise combination of the enumeration values that determines how the file will be shared by processes. The default value is <see cref="FileShare.Read" />.</param>
/// <param name="preallocationSize">The initial allocation size in bytes for the file. A positive value is effective only when a regular file is being created, overwritten, or replaced.
/// Negative values are not allowed. In other cases (including the default 0 value), it's ignored.</param>
/// <param name="options">An object that describes optional <see cref="Microsoft.Win32.SafeHandles.SafeFileHandle" /> parameters to use.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="path" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="path" /> is an empty string (""), contains only white space, or contains one or more invalid characters.
/// -or-
/// <paramref name="path" /> refers to a non-file device, such as <c>CON:</c>, <c>COM1:</c>, <c>LPT1:</c>, etc. in an NTFS environment.</exception>
/// <exception cref="T:System.NotSupportedException"><paramref name="path" /> refers to a non-file device, such as <c>CON:</c>, <c>COM1:</c>, <c>LPT1:</c>, etc. in a non-NTFS environment.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="preallocationSize" /> is negative.
/// -or-
/// <paramref name="mode" />, <paramref name="access" />, or <paramref name="share" /> contain an invalid value.</exception>
/// <exception cref="T:System.IO.FileNotFoundException">The file cannot be found, such as when <paramref name="mode" /> is <see cref="FileMode.Truncate" /> or <see cref="FileMode.Open" />, and the file specified by <paramref name="path" /> does not exist. The file must already exist in these modes.</exception>
/// <exception cref="T:System.IO.IOException">An I/O error, such as specifying <see cref="FileMode.CreateNew" /> when the file specified by <paramref name="path" /> already exists, occurred.
/// -or-
/// The disk was full (when <paramref name="preallocationSize" /> was provided and <paramref name="path" /> was pointing to a regular file).
/// -or-
/// The file was too large (when <paramref name="preallocationSize" /> was provided and <paramref name="path" /> was pointing to a regular file).</exception>
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// <exception cref="T:System.IO.DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive.</exception>
/// <exception cref="T:System.UnauthorizedAccessException">The <paramref name="access" /> requested is not permitted by the operating system for the specified <paramref name="path" />, such as when <paramref name="access" /> is <see cref="FileAccess.Write" /> or <see cref="FileAccess.ReadWrite" /> and the file or directory is set for read-only access.
/// -or-
/// <see cref="F:System.IO.FileOptions.Encrypted" /> is specified for <paramref name="options" />, but file encryption is not supported on the current platform.</exception>
/// <exception cref="T:System.IO.PathTooLongException">The specified path, file name, or both exceed the system-defined maximum length. </exception>
/// <summary>Caches whether Serialization Guard has been disabled for file writes</summary>
privatestaticints_cachedSerializationSwitch;
privatereadonlyFileStreamStrategy_strategy;
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead. https://go.microsoft.com/fwlink/?linkid=14202")]
[Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202")]
[Obsolete("This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead, and optionally make a new SafeFileHandle with ownsHandle=false if needed. https://go.microsoft.com/fwlink/?linkid=14202")]
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
usingSystem.Collections.Generic;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingMicrosoft.Win32.SafeHandles;
namespaceSystem.IO
{
publicstaticpartialclassRandomAccess
{
/// <summary>
/// Gets the length of the file in bytes.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <returns>A long value representing the length of the file in bytes.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
publicstaticlongGetLength(SafeFileHandlehandle)
{
ValidateInput(handle,fileOffset:0);
returnGetFileLength(handle,path:null);
}
/// <summary>
/// Reads a sequence of bytes from given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffer">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the file.</param>
/// <param name="fileOffset">The file position to read from.</param>
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the file has been reached.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for reading.</exception>
/// Reads a sequence of bytes from given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffers">A list of memory buffers. When this method returns, the contents of the buffers are replaced by the bytes read from the file.</param>
/// <param name="fileOffset">The file position to read from.</param>
/// <returns>The total number of bytes read into the buffers. This can be less than the number of bytes allocated in the buffers if that many bytes are not currently available, or zero (0) if the end of the file has been reached.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> or <paramref name="buffers" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for reading.</exception>
/// Reads a sequence of bytes from given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffer">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the file.</param>
/// <param name="fileOffset">The file position to read from.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the file has been reached.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was not opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for reading.</exception>
/// Reads a sequence of bytes from given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffers">A list of memory buffers. When this method returns, the contents of these buffers are replaced by the bytes read from the file.</param>
/// <param name="fileOffset">The file position to read from.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
/// <returns>The total number of bytes read into the buffers. This can be less than the number of bytes allocated in the buffers if that many bytes are not currently available, or zero (0) if the end of the file has been reached.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> or <paramref name="buffers" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was not opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for reading.</exception>
/// Writes a sequence of bytes from given buffer to given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffer">A region of memory. This method copies the contents of this region to the file.</param>
/// <param name="fileOffset">The file position to write to.</param>
/// <returns>The total number of bytes written into the file. This can be less than the number of bytes provided in the buffer and it's not an error.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for writing.</exception>
/// Writes a sequence of bytes from given buffers to given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffers">A list of memory buffers. This method copies the contents of these buffers to the file.</param>
/// <param name="fileOffset">The file position to write to.</param>
/// <returns>The total number of bytes written into the file. This can be less than the number of bytes provided in the buffers and it's not an error.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> or <paramref name="buffers" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for writing.</exception>
/// Writes a sequence of bytes from given buffer to given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffer">A region of memory. This method copies the contents of this region to the file.</param>
/// <param name="fileOffset">The file position to write to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
/// <returns>The total number of bytes written into the file. This can be less than the number of bytes provided in the buffer and it's not an error.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was not opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for writing.</exception>
/// Writes a sequence of bytes from given buffers to given file at given offset.
/// </summary>
/// <param name="handle">The file handle.</param>
/// <param name="buffers">A list of memory buffers. This method copies the contents of these buffers to the file.</param>
/// <param name="fileOffset">The file position to write to.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
/// <returns>The total number of bytes written into the file. This can be less than the number of bytes provided in the buffers and it's not an error.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="handle" /> or <paramref name="buffers"/> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> is invalid.</exception>
/// <exception cref="T:System.ObjectDisposedException">The file is closed.</exception>
/// <exception cref="T:System.NotSupportedException">The file does not support seeking (pipe or socket).</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="handle" /> was not opened for async IO.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="fileOffset" /> is negative.</exception>
/// <exception cref="T:System.UnauthorizedAccessException"><paramref name="handle" /> was not opened for writing.</exception>
longpos=CheckFileCall(Interop.Sys.LSeek(fileHandle,offset,(Interop.Sys.SeekWhence)(int)origin));// SeekOrigin values are the same as Interop.libc.SeekWhence values
longpos=FileStreamHelpers.CheckFileCall(Interop.Sys.LSeek(fileHandle,offset,(Interop.Sys.SeekWhence)(int)origin),_path);// SeekOrigin values are the same as Interop.libc.SeekWhence values
if(!_isPipe)// avoid async buffering with pipes, as doing so can lead to deadlocks (see comments in ReadInternalAsyncCore)
if(!_fileHandle.IsPipe)// avoid async buffering with pipes, as doing so can lead to deadlocks (see comments in ReadInternalAsyncCore)
{
// Ensure the buffer is clear for writing
if(_writePos==0)
...
...
@@ -1068,14 +1005,14 @@ private unsafe int ReadFileNative(SafeFileHandle handle, Span<byte> bytes, Nativ
{
Debug.Assert((_useAsyncIO&&overlapped!=null)||(!_useAsyncIO&&overlapped==null),"Async IO and overlapped parameters inconsistent in call to ReadFileNative.");
Debug.Assert((_useAsyncIO&&overlapped!=null)||(!_useAsyncIO&&overlapped==null),"Async IO and overlapped parameters inconsistent in call to WriteFileNative.");
public static long GetLength(Microsoft.Win32.SafeHandles.SafeFileHandle handle) { throw null; }
public static int Read(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Span<byte> buffer, long fileOffset) { throw null; }
public static long Read(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Collections.Generic.IReadOnlyList<System.Memory<byte>> buffers, long fileOffset) { throw null; }
public static System.Threading.Tasks.ValueTask<int> ReadAsync(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Memory<byte> buffer, long fileOffset, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.ValueTask<long> ReadAsync(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Collections.Generic.IReadOnlyList<System.Memory<byte>> buffers, long fileOffset, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static int Write(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.ReadOnlySpan<byte> buffer, long fileOffset) { throw null; }
public static long Write(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Collections.Generic.IReadOnlyList<System.ReadOnlyMemory<byte>> buffers, long fileOffset) { throw null; }
public static System.Threading.Tasks.ValueTask<int> WriteAsync(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.ReadOnlyMemory<byte> buffer, long fileOffset, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.ValueTask<long> WriteAsync(Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Collections.Generic.IReadOnlyList<System.ReadOnlyMemory<byte>> buffers, long fileOffset, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }