Skip to content

Commit 8536eb1

Browse files
Revert "Override ReadAsync and WriteAsync methods on ConsoleStream. (#71971)" (#77382)
This reverts commit 8cb4e93. Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 530976e commit 8536eb1

File tree

6 files changed

+14
-196
lines changed

6 files changed

+14
-196
lines changed

src/libraries/System.Console/src/System/IO/ConsoleStream.cs

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System.Diagnostics;
55
using System.Runtime.InteropServices;
6-
using System.Threading;
7-
using System.Threading.Tasks;
86

97
namespace System.IO
108
{
@@ -30,46 +28,6 @@ public override void Write(byte[] buffer, int offset, int count)
3028

3129
public override void WriteByte(byte value) => Write(new ReadOnlySpan<byte>(in value));
3230

33-
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
34-
{
35-
ValidateWrite(buffer, offset, count);
36-
37-
if (cancellationToken.IsCancellationRequested)
38-
{
39-
return Task.FromCanceled(cancellationToken);
40-
}
41-
42-
try
43-
{
44-
Write(new ReadOnlySpan<byte>(buffer, offset, count));
45-
return Task.CompletedTask;
46-
}
47-
catch (Exception ex)
48-
{
49-
return Task.FromException(ex);
50-
}
51-
}
52-
53-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
54-
{
55-
ValidateCanWrite();
56-
57-
if (cancellationToken.IsCancellationRequested)
58-
{
59-
return ValueTask.FromCanceled(cancellationToken);
60-
}
61-
62-
try
63-
{
64-
Write(buffer.Span);
65-
return ValueTask.CompletedTask;
66-
}
67-
catch (Exception ex)
68-
{
69-
return ValueTask.FromException(ex);
70-
}
71-
}
72-
7331
public override int Read(byte[] buffer, int offset, int count)
7432
{
7533
ValidateRead(buffer, offset, count);
@@ -83,44 +41,6 @@ public override int ReadByte()
8341
return result != 0 ? b : -1;
8442
}
8543

86-
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
87-
{
88-
ValidateRead(buffer, offset, count);
89-
90-
if (cancellationToken.IsCancellationRequested)
91-
{
92-
return Task.FromCanceled<int>(cancellationToken);
93-
}
94-
95-
try
96-
{
97-
return Task.FromResult(Read(new Span<byte>(buffer, offset, count)));
98-
}
99-
catch (Exception exception)
100-
{
101-
return Task.FromException<int>(exception);
102-
}
103-
}
104-
105-
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
106-
{
107-
ValidateCanRead();
108-
109-
if (cancellationToken.IsCancellationRequested)
110-
{
111-
return ValueTask.FromCanceled<int>(cancellationToken);
112-
}
113-
114-
try
115-
{
116-
return ValueTask.FromResult(Read(buffer.Span));
117-
}
118-
catch (Exception exception)
119-
{
120-
return ValueTask.FromException<int>(exception);
121-
}
122-
}
123-
12444
protected override void Dispose(bool disposing)
12545
{
12646
_canRead = false;
@@ -151,11 +71,7 @@ public override void Flush() { }
15171
protected void ValidateRead(byte[] buffer, int offset, int count)
15272
{
15373
ValidateBufferArguments(buffer, offset, count);
154-
ValidateCanRead();
155-
}
15674

157-
private void ValidateCanRead()
158-
{
15975
if (!_canRead)
16076
{
16177
throw Error.GetReadNotSupported();
@@ -165,11 +81,7 @@ private void ValidateCanRead()
16581
protected void ValidateWrite(byte[] buffer, int offset, int count)
16682
{
16783
ValidateBufferArguments(buffer, offset, count);
168-
ValidateCanWrite();
169-
}
17084

171-
private void ValidateCanWrite()
172-
{
17385
if (!_canWrite)
17486
{
17587
throw Error.GetWriteNotSupported();

src/libraries/System.Console/tests/ConsoleStreamTests.cs

Lines changed: 0 additions & 99 deletions
This file was deleted.

src/libraries/System.Console/tests/Helpers.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
using System.Text;
77
using Xunit;
88

9-
static class Helpers
9+
class Helpers
1010
{
11-
public static bool IsConsoleInSupported =>
12-
!PlatformDetection.IsAndroid && !PlatformDetection.IsiOS && !PlatformDetection.IsMacCatalyst && !PlatformDetection.IstvOS && !PlatformDetection.IsBrowser;
13-
1411
public static void SetAndReadHelper(Action<TextWriter> setHelper, Func<TextWriter> getHelper, Func<StreamReader, string> readHelper)
1512
{
1613
const string TestString = "Test";

src/libraries/System.Console/tests/ReadAndWrite.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public static void WriteOverloads()
3131
}
3232
}
3333

34+
[Fact]
35+
public static void WriteToOutputStream_EmptyArray()
36+
{
37+
Stream outStream = Console.OpenStandardOutput();
38+
outStream.Write(new byte[] { }, 0, 0);
39+
}
40+
3441
[Fact]
3542
[OuterLoop]
3643
public static void WriteOverloadsToRealConsole()

src/libraries/System.Console/tests/SetIn.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//
1111
public class SetIn
1212
{
13-
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
13+
[Fact]
14+
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
1415
public static void SetInThrowsOnNull()
1516
{
1617
TextReader savedIn = Console.In;
@@ -24,7 +25,8 @@ public static void SetInThrowsOnNull()
2425
}
2526
}
2627

27-
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
28+
[Fact]
29+
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
2830
public static void SetInReadLine()
2931
{
3032
const string TextStringFormat = "Test {0}";

src/libraries/System.Console/tests/System.Console.Tests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</PropertyGroup>
99
<ItemGroup>
1010
<Compile Include="CancelKeyPress.cs" />
11-
<Compile Include="ConsoleStreamTests.cs" />
1211
<Compile Include="Helpers.cs" />
1312
<Compile Include="ReadAndWrite.cs" />
1413
<Compile Include="ConsoleKeyInfoTests.cs" />
@@ -44,8 +43,8 @@
4443
</ItemGroup>
4544
<ItemGroup>
4645
<Content Include="$(MSBuildThisFileDirectory)TestData\**\*"
47-
Link="%(RecursiveDir)%(Filename)%(Extension)"
48-
CopyToOutputDirectory="PreserveNewest" />
46+
Link="%(RecursiveDir)%(Filename)%(Extension)"
47+
CopyToOutputDirectory="PreserveNewest" />
4948
</ItemGroup>
5049
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
5150
<Compile Include="ConsoleEncoding.Windows.cs" />

0 commit comments

Comments
 (0)