mscorlib(4.0.0.0) API with additions
__ConsoleStream.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
5 using System.Security;
6 
7 namespace System.IO
8 {
9  internal sealed class __ConsoleStream : Stream
10  {
11  private const int BytesPerWChar = 2;
12 
13  [SecurityCritical]
14  private SafeFileHandle _handle;
15 
16  private bool _canRead;
17 
18  private bool _canWrite;
19 
20  private bool _useFileAPIs;
21 
22  private bool _isPipe;
23 
24  public override bool CanRead => _canRead;
25 
26  public override bool CanWrite => _canWrite;
27 
28  public override bool CanSeek => false;
29 
30  public override long Length
31  {
32  get
33  {
34  __Error.SeekNotSupported();
35  return 0L;
36  }
37  }
38 
39  public override long Position
40  {
41  get
42  {
43  __Error.SeekNotSupported();
44  return 0L;
45  }
46  set
47  {
48  __Error.SeekNotSupported();
49  }
50  }
51 
52  [SecurityCritical]
53  internal __ConsoleStream(SafeFileHandle handle, FileAccess access, bool useFileAPIs)
54  {
55  _handle = handle;
56  _canRead = ((access & FileAccess.Read) == FileAccess.Read);
57  _canWrite = ((access & FileAccess.Write) == FileAccess.Write);
58  _useFileAPIs = useFileAPIs;
59  _isPipe = (Win32Native.GetFileType(handle) == 3);
60  }
61 
62  [SecuritySafeCritical]
63  protected override void Dispose(bool disposing)
64  {
65  if (_handle != null)
66  {
67  _handle = null;
68  }
69  _canRead = false;
70  _canWrite = false;
71  base.Dispose(disposing);
72  }
73 
74  [SecuritySafeCritical]
75  public override void Flush()
76  {
77  if (_handle == null)
78  {
79  __Error.FileNotOpen();
80  }
81  if (!CanWrite)
82  {
83  __Error.WriteNotSupported();
84  }
85  }
86 
87  public override void SetLength(long value)
88  {
89  __Error.SeekNotSupported();
90  }
91 
92  [SecuritySafeCritical]
93  public override int Read([In] [Out] byte[] buffer, int offset, int count)
94  {
95  if (buffer == null)
96  {
97  throw new ArgumentNullException("buffer");
98  }
99  if (offset < 0 || count < 0)
100  {
101  throw new ArgumentOutOfRangeException((offset < 0) ? "offset" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
102  }
103  if (buffer.Length - offset < count)
104  {
105  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
106  }
107  if (!_canRead)
108  {
109  __Error.ReadNotSupported();
110  }
111  int bytesRead;
112  int num = ReadFileNative(_handle, buffer, offset, count, _useFileAPIs, _isPipe, out bytesRead);
113  if (num != 0)
114  {
115  __Error.WinIOError(num, string.Empty);
116  }
117  return bytesRead;
118  }
119 
120  public override long Seek(long offset, SeekOrigin origin)
121  {
122  __Error.SeekNotSupported();
123  return 0L;
124  }
125 
126  [SecuritySafeCritical]
127  public override void Write(byte[] buffer, int offset, int count)
128  {
129  if (buffer == null)
130  {
131  throw new ArgumentNullException("buffer");
132  }
133  if (offset < 0 || count < 0)
134  {
135  throw new ArgumentOutOfRangeException((offset < 0) ? "offset" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
136  }
137  if (buffer.Length - offset < count)
138  {
139  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
140  }
141  if (!_canWrite)
142  {
143  __Error.WriteNotSupported();
144  }
145  int num = WriteFileNative(_handle, buffer, offset, count, _useFileAPIs);
146  if (num != 0)
147  {
148  __Error.WinIOError(num, string.Empty);
149  }
150  }
151 
152  [SecurityCritical]
153  private unsafe static int ReadFileNative(SafeFileHandle hFile, byte[] bytes, int offset, int count, bool useFileAPIs, bool isPipe, out int bytesRead)
154  {
155  if (bytes.Length - offset < count)
156  {
157  throw new IndexOutOfRangeException(Environment.GetResourceString("IndexOutOfRange_IORaceCondition"));
158  }
159  if (bytes.Length == 0)
160  {
161  bytesRead = 0;
162  return 0;
163  }
164  WaitForAvailableConsoleInput(hFile, isPipe);
165  bool flag;
166  if (useFileAPIs)
167  {
168  fixed (byte* ptr = bytes)
169  {
170  flag = (Win32Native.ReadFile(hFile, ptr + offset, count, out bytesRead, IntPtr.Zero) != 0);
171  }
172  }
173  else
174  {
175  fixed (byte* ptr2 = bytes)
176  {
177  flag = Win32Native.ReadConsoleW(hFile, ptr2 + offset, count / 2, out int lpNumberOfCharsRead, IntPtr.Zero);
178  bytesRead = lpNumberOfCharsRead * 2;
179  }
180  }
181  if (flag)
182  {
183  return 0;
184  }
185  int lastWin32Error = Marshal.GetLastWin32Error();
186  if (lastWin32Error == 232 || lastWin32Error == 109)
187  {
188  return 0;
189  }
190  return lastWin32Error;
191  }
192 
193  [SecurityCritical]
194  private unsafe static int WriteFileNative(SafeFileHandle hFile, byte[] bytes, int offset, int count, bool useFileAPIs)
195  {
196  if (bytes.Length == 0)
197  {
198  return 0;
199  }
200  bool flag;
201  if (useFileAPIs)
202  {
203  fixed (byte* ptr = bytes)
204  {
205  flag = (Win32Native.WriteFile(hFile, ptr + offset, count, out int _, IntPtr.Zero) != 0);
206  }
207  }
208  else
209  {
210  fixed (byte* ptr2 = bytes)
211  {
212  flag = Win32Native.WriteConsoleW(hFile, ptr2 + offset, count / 2, out int _, IntPtr.Zero);
213  }
214  }
215  if (flag)
216  {
217  return 0;
218  }
219  int lastWin32Error = Marshal.GetLastWin32Error();
220  if (lastWin32Error == 232 || lastWin32Error == 109)
221  {
222  return 0;
223  }
224  return lastWin32Error;
225  }
226 
227  [MethodImpl(MethodImplOptions.InternalCall)]
228  [SecurityCritical]
229  private static extern void WaitForAvailableConsoleInput(SafeFileHandle file, bool isPipe);
230  }
231 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Definition: __Canon.cs:3
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.
Definition: SeekOrigin.cs:9
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....
Definition: Environment.cs:21
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.
Definition: IntPtr.cs:14
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
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.
Definition: FileAccess.cs:9
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
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 ....
Definition: Stream.cs:16