-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamInputReader.cs
More file actions
44 lines (37 loc) · 1.32 KB
/
StreamInputReader.cs
File metadata and controls
44 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
namespace VTTest;
/// <summary>
/// Reads console input via a .NET <see cref="Stream"/> —
/// <see cref="Console.OpenStandardInput()"/> on Windows, or a raw fd-0
/// <see cref="FileStream"/> on Unix to bypass .NET's buffered console stream
/// (which requires a newline before returning data).
/// </summary>
internal sealed class StreamInputReader : IInputReader
{
private readonly bool _isWindows;
private Stream? _stdinStream;
internal StreamInputReader(bool isWindows)
{
_isWindows = isWindows;
}
public string Label => "Strm";
public string DisplayName => "Stream (Console.OpenStandardInput)";
public string ZeroBytesMessage => "[Ctrl+Z] (Stream returned 0 bytes - Windows EOF bug)";
public int Read(byte[] buffer)
{
// Lazily open the stream so disposal + re-creation is cheap when toggling modes.
_stdinStream ??= _isWindows
? Console.OpenStandardInput()
: new FileStream(
new SafeFileHandle((IntPtr)0, ownsHandle: false),
FileAccess.Read,
bufferSize: 1);
return _stdinStream.Read(buffer, 0, buffer.Length);
}
public void Dispose()
{
_stdinStream?.Dispose();
_stdinStream = null;
}
}