Skip to content

Commit

Permalink
Revert "Override ReadAsync and WriteAsync methods on ConsoleStream. (#…
Browse files Browse the repository at this point in the history
…71971)" (#77360)

This reverts commit 8cb4e93.
  • Loading branch information
stephentoub authored Oct 24, 2022
1 parent 1c265ec commit 58a1180
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 196 deletions.
88 changes: 0 additions & 88 deletions src/libraries/System.Console/src/System/IO/ConsoleStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

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

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

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ValidateWrite(buffer, offset, count);

if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

try
{
Write(new ReadOnlySpan<byte>(buffer, offset, count));
return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException(ex);
}
}

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
ValidateCanWrite();

if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled(cancellationToken);
}

try
{
Write(buffer.Span);
return ValueTask.CompletedTask;
}
catch (Exception ex)
{
return ValueTask.FromException(ex);
}
}

public override int Read(byte[] buffer, int offset, int count)
{
ValidateRead(buffer, offset, count);
Expand All @@ -83,44 +41,6 @@ public override int ReadByte()
return result != 0 ? b : -1;
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ValidateRead(buffer, offset, count);

if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<int>(cancellationToken);
}

try
{
return Task.FromResult(Read(new Span<byte>(buffer, offset, count)));
}
catch (Exception exception)
{
return Task.FromException<int>(exception);
}
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
ValidateCanRead();

if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled<int>(cancellationToken);
}

try
{
return ValueTask.FromResult(Read(buffer.Span));
}
catch (Exception exception)
{
return ValueTask.FromException<int>(exception);
}
}

protected override void Dispose(bool disposing)
{
_canRead = false;
Expand Down Expand Up @@ -151,11 +71,7 @@ public override void Flush() { }
protected void ValidateRead(byte[] buffer, int offset, int count)
{
ValidateBufferArguments(buffer, offset, count);
ValidateCanRead();
}

private void ValidateCanRead()
{
if (!_canRead)
{
throw Error.GetReadNotSupported();
Expand All @@ -165,11 +81,7 @@ private void ValidateCanRead()
protected void ValidateWrite(byte[] buffer, int offset, int count)
{
ValidateBufferArguments(buffer, offset, count);
ValidateCanWrite();
}

private void ValidateCanWrite()
{
if (!_canWrite)
{
throw Error.GetWriteNotSupported();
Expand Down
99 changes: 0 additions & 99 deletions src/libraries/System.Console/tests/ConsoleStreamTests.cs

This file was deleted.

5 changes: 1 addition & 4 deletions src/libraries/System.Console/tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
using System.Text;
using Xunit;

static class Helpers
class Helpers
{
public static bool IsConsoleInSupported =>
!PlatformDetection.IsAndroid && !PlatformDetection.IsiOS && !PlatformDetection.IsMacCatalyst && !PlatformDetection.IstvOS && !PlatformDetection.IsBrowser;

public static void SetAndReadHelper(Action<TextWriter> setHelper, Func<TextWriter> getHelper, Func<StreamReader, string> readHelper)
{
const string TestString = "Test";
Expand Down
7 changes: 7 additions & 0 deletions src/libraries/System.Console/tests/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public static void WriteOverloads()
}
}

[Fact]
public static void WriteToOutputStream_EmptyArray()
{
Stream outStream = Console.OpenStandardOutput();
outStream.Write(new byte[] { }, 0, 0);
}

[Fact]
[OuterLoop]
public static void WriteOverloadsToRealConsole()
Expand Down
6 changes: 4 additions & 2 deletions src/libraries/System.Console/tests/SetIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//
public class SetIn
{
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void SetInThrowsOnNull()
{
TextReader savedIn = Console.In;
Expand All @@ -24,7 +25,8 @@ public static void SetInThrowsOnNull()
}
}

[ConditionalFact(typeof(Helpers), nameof(Helpers.IsConsoleInSupported))]
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void SetInReadLine()
{
const string TextStringFormat = "Test {0}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="CancelKeyPress.cs" />
<Compile Include="ConsoleStreamTests.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="ReadAndWrite.cs" />
<Compile Include="ConsoleKeyInfoTests.cs" />
Expand Down Expand Up @@ -44,8 +43,8 @@
</ItemGroup>
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)TestData\**\*"
Link="%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
Link="%(RecursiveDir)%(Filename)%(Extension)"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<Compile Include="ConsoleEncoding.Windows.cs" />
Expand Down

0 comments on commit 58a1180

Please sign in to comment.