2 using Microsoft.Win32.SafeHandles;
9 internal sealed
class __ConsoleStream :
Stream 11 private const int BytesPerWChar = 2;
14 private SafeFileHandle _handle;
16 private bool _canRead;
18 private bool _canWrite;
20 private bool _useFileAPIs;
24 public override bool CanRead => _canRead;
26 public override bool CanWrite => _canWrite;
28 public override bool CanSeek =>
false;
30 public override long Length
34 __Error.SeekNotSupported();
39 public override long Position
43 __Error.SeekNotSupported();
48 __Error.SeekNotSupported();
53 internal __ConsoleStream(SafeFileHandle handle,
FileAccess access,
bool useFileAPIs)
58 _useFileAPIs = useFileAPIs;
59 _isPipe = (Win32Native.GetFileType(handle) == 3);
62 [SecuritySafeCritical]
63 protected override void Dispose(
bool disposing)
71 base.Dispose(disposing);
74 [SecuritySafeCritical]
75 public override void Flush()
79 __Error.FileNotOpen();
83 __Error.WriteNotSupported();
87 public override void SetLength(
long value)
89 __Error.SeekNotSupported();
92 [SecuritySafeCritical]
93 public override int Read([In] [Out]
byte[] buffer,
int offset,
int count)
99 if (offset < 0 || count < 0)
103 if (buffer.Length - offset < count)
109 __Error.ReadNotSupported();
112 int num = ReadFileNative(_handle, buffer, offset, count, _useFileAPIs, _isPipe, out bytesRead);
115 __Error.WinIOError(num,
string.Empty);
120 public override long Seek(
long offset,
SeekOrigin origin)
122 __Error.SeekNotSupported();
126 [SecuritySafeCritical]
127 public override void Write(
byte[] buffer,
int offset,
int count)
133 if (offset < 0 || count < 0)
137 if (buffer.Length - offset < count)
143 __Error.WriteNotSupported();
145 int num = WriteFileNative(_handle, buffer, offset, count, _useFileAPIs);
148 __Error.WinIOError(num,
string.Empty);
153 private unsafe
static int ReadFileNative(SafeFileHandle hFile,
byte[] bytes,
int offset,
int count,
bool useFileAPIs,
bool isPipe, out
int bytesRead)
155 if (bytes.Length - offset < count)
159 if (bytes.Length == 0)
164 WaitForAvailableConsoleInput(hFile, isPipe);
168 fixed (
byte* ptr = bytes)
170 flag = (Win32Native.ReadFile(hFile, ptr + offset, count, out bytesRead,
IntPtr.
Zero) != 0);
175 fixed (
byte* ptr2 = bytes)
177 flag = Win32Native.ReadConsoleW(hFile, ptr2 + offset, count / 2, out
int lpNumberOfCharsRead,
IntPtr.
Zero);
178 bytesRead = lpNumberOfCharsRead * 2;
186 if (lastWin32Error == 232 || lastWin32Error == 109)
190 return lastWin32Error;
194 private unsafe
static int WriteFileNative(SafeFileHandle hFile,
byte[] bytes,
int offset,
int count,
bool useFileAPIs)
196 if (bytes.Length == 0)
203 fixed (
byte* ptr = bytes)
205 flag = (Win32Native.WriteFile(hFile, ptr + offset, count, out
int _,
IntPtr.
Zero) != 0);
210 fixed (
byte* ptr2 = bytes)
212 flag = Win32Native.WriteConsoleW(hFile, ptr2 + offset, count / 2, out
int _,
IntPtr.
Zero);
220 if (lastWin32Error == 232 || lastWin32Error == 109)
224 return lastWin32Error;
229 private static extern void WaitForAvailableConsoleInput(SafeFileHandle file,
bool isPipe);
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
SeekOrigin
Specifies the position in a stream to use for seeking.
Read access to the file. Data can be read from the file. Combine with Write for read/write access.
Write access to the file. Data can be written to the file. Combine with Read for read/write access.
Provides information about, and means to manipulate, the current environment and platform....
The exception that is thrown when an attempt is made to access an element of an array or collection w...
A platform-specific type that is used to represent a pointer or a handle.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
MethodImplOptions
Defines the details of how a method is implemented.
The exception that is thrown when one of the arguments provided to a method is not valid.
FileAccess
Defines constants for read, write, or read/write access to a file.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....