protected override void Dispose(bool disposing) { // We're probably better off not closing the OS handle here. First, // we allow a program to get multiple instances of __ConsoleStreams // around the same OS handle, so closing one handle would invalidate // them all. Additionally, we want a second AppDomain to be able to // write to stdout if a second AppDomain quits. if (_handle != null) { _handle = null; } _canRead = false; _canWrite = false; base.Dispose(disposing); }
internal __ConsoleStream(SafeFileHandle handle, FileAccess access) { _handle = handle; _canRead = ((access & FileAccess.Read) == FileAccess.Read); _canWrite = ((access & FileAccess.Write) == FileAccess.Write); }
private static int WriteFileNative(SafeFileHandle hFile, byte[] bytes, int offset, int count) { // You can't use the fixed statement on an array of length 0. if (bytes.Length == 0) return ERROR_SUCCESS; bool writeSuccess; int numBytesWritten = hFile.WriteFile(bytes, offset, count); writeSuccess = (0 != numBytesWritten); if (writeSuccess) return ERROR_SUCCESS; int errorCode = -1; return errorCode; }
/// <summary> /// </summary> /// <param name="path"> /// </param> /// <param name="access"> /// </param> /// <param name="handle"> /// </param> /// <returns> /// </returns> private static FileStream OpenFile(string path, FileAccess access, out SafeFileHandle handle) { FileStream fs = new FileStream(path, FileMode.Open, access, FileShare.ReadWrite, 1); handle = fs.SafeFileHandle; if (handle.IsInvalid) { __Error.WinIOError(0, path); } return fs; }
private static int ReadFileNative(SafeFileHandle hFile, byte[] bytes, int offset, int count, out int bytesRead) { if (bytes.Length - offset < count) throw new IndexOutOfRangeException("IORaceCondition"); // You can't use the fixed statement on an array of length 0. if (bytes.Length == 0) { bytesRead = 0; return ERROR_SUCCESS; } bool readSuccess; bytesRead = hFile.ReadFile(bytes, offset, count); readSuccess = (0 != bytesRead); if (readSuccess) return ERROR_SUCCESS; int errorCode = -1; return errorCode; }
private static ValueTask <long> ReadScatterAtOffsetAsync(SafeFileHandle handle, IReadOnlyList <Memory <byte> > buffers, long fileOffset, CancellationToken cancellationToken) => ScheduleSyncReadScatterAtOffsetAsync(handle, buffers, fileOffset, cancellationToken);
private static ValueTask <int> ReadAtOffsetAsync(SafeFileHandle handle, Memory <byte> buffer, long fileOffset, CancellationToken cancellationToken) => ScheduleSyncReadAtOffsetAsync(handle, buffer, fileOffset, cancellationToken);
private static bool IsHandleInvalid(SafeFileHandle handle) { return(handle == null || handle.IsInvalid || handle.IsClosed); }
internal static bool GetDefaultIsAsync(SafeFileHandle handle, bool defaultIsAsync) => handle.IsAsync ?? defaultIsAsync;
// in the future we are most probably going to introduce more strategies (io_uring etc) internal static FileStreamStrategy ChooseStrategy(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) => new LegacyFileStreamStrategy(handle, access, bufferSize, isAsync);