Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 internal __ConsoleStream(SafeFileHandle handle, FileAccess access)
 {
     _handle = handle;
     _canRead = ((access & FileAccess.Read) == FileAccess.Read);
     _canWrite = ((access & FileAccess.Write) == FileAccess.Write);
 }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
 private static ValueTask <long> ReadScatterAtOffsetAsync(SafeFileHandle handle, IReadOnlyList <Memory <byte> > buffers,
                                                          long fileOffset, CancellationToken cancellationToken)
 => ScheduleSyncReadScatterAtOffsetAsync(handle, buffers, fileOffset, cancellationToken);
Exemplo n.º 7
0
 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);
 }
Exemplo n.º 9
0
 internal static bool GetDefaultIsAsync(SafeFileHandle handle, bool defaultIsAsync) => handle.IsAsync ?? defaultIsAsync;
Exemplo n.º 10
0
 // 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);