Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/libraries/System.Console/src/System/IO/ConsoleStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

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

namespace System.IO
{
Expand All @@ -28,6 +30,32 @@ 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)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

ValidateWrite(buffer, offset, count);
Write(new ReadOnlySpan<byte>(buffer, offset, count));

return Task.CompletedTask;
}

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled(cancellationToken);
}

ValidateCanWrite();
Write(buffer.Span);

return ValueTask.CompletedTask;
}

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

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<int>(cancellationToken);
}

ValidateRead(buffer, offset, count);
return Task.FromResult(Read(new Span<byte>(buffer, offset, count)));
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return ValueTask.FromCanceled<int>(cancellationToken);
}

ValidateCanRead();
return ValueTask.FromResult(Read(buffer.Span));
}

protected override void Dispose(bool disposing)
{
_canRead = false;
Expand Down Expand Up @@ -74,7 +124,11 @@ 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 @@ -84,7 +138,11 @@ protected void ValidateRead(byte[] buffer, int offset, int count)
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: 99 additions & 0 deletions src/libraries/System.Console/tests/ConsoleStreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

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

[Fact]
public void ReadAsyncRespectsCancellation()
{
Stream inStream = Console.OpenStandardInput();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();

byte[] buffer = new byte[1024];
Task result = inStream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
Assert.True(result.IsCanceled);

ValueTask<int> valueTaskResult = inStream.ReadAsync(buffer.AsMemory(), cts.Token);
Assert.True(valueTaskResult.IsCanceled);
}

[Fact]
public void ReadAsyncHandlesInvalidParams()
{
Stream inStream = Console.OpenStandardInput();

byte[] buffer = new byte[1024];
Assert.Throws<ArgumentNullException>(() => { inStream.ReadAsync(null, 0, buffer.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { inStream.ReadAsync(buffer, -1, buffer.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { inStream.ReadAsync(buffer, 0, buffer.Length + 1); });
}

[Fact]
public void WriteAsyncRespectsCancellation()
{
Stream outStream = Console.OpenStandardOutput();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();

byte[] bytes = Encoding.ASCII.GetBytes("Hi");
Task result = outStream.WriteAsync(bytes, 0, bytes.Length, cts.Token);
Assert.True(result.IsCanceled);

ValueTask valueTaskResult = outStream.WriteAsync(bytes.AsMemory(), cts.Token);
Assert.True(valueTaskResult.IsCanceled);
}

[Fact]
public void WriteAsyncHandlesInvalidParams()
{
Stream outStream = Console.OpenStandardOutput();

byte[] bytes = Encoding.ASCII.GetBytes("Hi");
Assert.Throws<ArgumentNullException>(() => { outStream.WriteAsync(null, 0, bytes.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { outStream.WriteAsync(bytes, -1, bytes.Length); });
Assert.Throws<ArgumentOutOfRangeException>(() => { outStream.WriteAsync(bytes, 0, bytes.Length + 1); });
}

[Fact]
public void InputCannotWriteAsync()
{
Stream inStream = Console.OpenStandardInput();

byte[] bytes = Encoding.ASCII.GetBytes("Hi");
Assert.Throws<NotSupportedException>(() => { inStream.WriteAsync(bytes, 0, bytes.Length); });

Assert.Throws<NotSupportedException>(() => { inStream.WriteAsync(bytes.AsMemory()); });
}

[Fact]
public void OutputCannotReadAsync()
{
Stream outStream = Console.OpenStandardOutput();

byte[] buffer = new byte[1024];
Assert.Throws<NotSupportedException>(() =>
{
outStream.ReadAsync(buffer, 0, buffer.Length);
});

Assert.Throws<NotSupportedException>(() =>
{
outStream.ReadAsync(buffer.AsMemory());
});
}
}
7 changes: 0 additions & 7 deletions src/libraries/System.Console/tests/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</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 @@ -36,8 +37,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