Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
71b3888
Fix Console.Write on iOS
MaximLipnin Aug 2, 2021
8f6f7bc
Move the helper method to iOS-specific location
MaximLipnin Aug 2, 2021
a97f0d5
Add a test
MaximLipnin Aug 2, 2021
90bf33c
Remove unused using
MaximLipnin Aug 2, 2021
e6806e4
Apply Ralf's suggestion
MaximLipnin Aug 2, 2021
2bdb6e7
Move Interop.Sys.Log to a local function
MaximLipnin Aug 2, 2021
612dc1c
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 3, 2021
6f8542b
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 9, 2021
47d8f01
Move the caching logic over to StreamWriter
MaximLipnin Aug 10, 2021
4f0653a
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 10, 2021
14545a8
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 11, 2021
725e64c
feedback
MaximLipnin Aug 11, 2021
5b8499c
Remove unused usings
MaximLipnin Aug 12, 2021
61bb68b
Derive from StreamWriter
MaximLipnin Aug 12, 2021
68e10e7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 12, 2021
ac45868
Feedback
MaximLipnin Aug 12, 2021
2e57d54
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 16, 2021
c8aa30f
Try a solution not changing public API surface and using NSLogStream …
MaximLipnin Aug 16, 2021
b76ddfe
Some feedback
MaximLipnin Aug 17, 2021
b999fff
Use ArrayPool
MaximLipnin Aug 17, 2021
49f5c41
Not rely on the length of the rented array
MaximLipnin Aug 17, 2021
aaa1f35
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 18, 2021
90da2a0
Address the feedback
MaximLipnin Aug 27, 2021
88685c7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 27, 2021
976954b
not use _ prefix for local variable
MaximLipnin Aug 27, 2021
245edb3
Address the feedback
MaximLipnin Aug 27, 2021
a234df5
Merge branch 'fix_console_write_on_ios' of https://github.com/maximli…
MaximLipnin Aug 27, 2021
34d215f
Fix an error
MaximLipnin Aug 30, 2021
1eb7528
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 30, 2021
a2e118e
Print a span rather than a string
MaximLipnin Aug 30, 2021
732a62f
Slice the char span to the exact used length
MaximLipnin Sep 1, 2021
9375848
Address Stephen's feedback
MaximLipnin Sep 1, 2021
7504d30
Remove a redundant using
MaximLipnin Sep 1, 2021
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
1 change: 1 addition & 0 deletions src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Link="Common\Interop\Unix\Interop.Log.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="System\IO\StreamWriter.iOS.cs" />
</ItemGroup>
<!-- Android -->
<ItemGroup Condition="'$(TargetsAndroid)' == 'true'">
Expand Down
9 changes: 9 additions & 0 deletions src/libraries/System.Console/src/System/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ private static TextWriter CreateOutputWriter(Stream outputStream)
{
return TextWriter.Synchronized(outputStream == Stream.Null ?
StreamWriter.Null :
(OperatingSystem.IsIOS() || OperatingSystem.IsTvOS() || OperatingSystem.IsMacCatalyst()) ?
new IOSStreamWriter(
stream: outputStream,
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
bufferSize: WriteBufferSize,
leaveOpen: true)
{
AutoFlush = true
} :
new StreamWriter(
stream: outputStream,
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
Expand Down
104 changes: 104 additions & 0 deletions src/libraries/System.Console/src/System/IO/StreamWriter.iOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace System.IO
{
internal class IOSStreamWriter : StreamWriter
{
private readonly List<byte> _acc = new List<byte>();
private int _lineStartPos;

public IOSStreamWriter(Stream stream, Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false)
: base(stream, encoding, bufferSize, leaveOpen)
{
}

protected override void Flush(bool flushStream, bool flushEncoder)
{
// flushEncoder should be true at the end of the file and if
// the user explicitly calls Flush (though not if AutoFlush is true).
// This is required to flush any dangling characters from our UTF-7
// and UTF-8 encoders.
ThrowIfDisposed();

// Perf boost for Flush on non-dirty writers.
if (innerCharPos == 0 && !flushStream && !flushEncoder)
{
return;
}

if (!innerHaveWrittenPreamble)
{
innerHaveWrittenPreamble = true;
ReadOnlySpan<byte> preamble = innerEncoding.Preamble;
if (preamble.Length > 0)
{
innerStream.Write(preamble);
}
}

// For sufficiently small char data being flushed, try to encode to the stack.
// For anything else, fall back to allocating the byte[] buffer.
Span<byte> byteBuffer = stackalloc byte[0];
if (innerByteBuffer is not null)
{
byteBuffer = innerByteBuffer;
}
else
{
int maxBytesForCharPos = innerEncoding.GetMaxByteCount(innerCharPos);
byteBuffer = maxBytesForCharPos <= 1024 ? // arbitrary threshold
stackalloc byte[1024] :
(innerByteBuffer = new byte[innerEncoding.GetMaxByteCount(innerCharBuffer.Length)]);
}

for (int i = _lineStartPos; i < innerCharPos; i++)
{
if (innerCharBuffer[i] == '\n')
{
int count = innerEncoder.GetBytes(new ReadOnlySpan<char>(innerCharBuffer, _lineStartPos, i - _lineStartPos), byteBuffer, flushEncoder);
if (count > 0)
{
_acc.AddRange(byteBuffer.Slice(0, count).ToArray());
}

innerStream.Write(CollectionsMarshal.AsSpan(_acc));
_acc.Clear();

_lineStartPos = i + 1;
}
}

if (_lineStartPos < innerCharPos)
{
int count = innerEncoder.GetBytes(new ReadOnlySpan<char>(innerCharBuffer, _lineStartPos, innerCharPos - _lineStartPos), byteBuffer, flushEncoder);
if (count > 0)
{
_acc.AddRange(byteBuffer.Slice(0, count).ToArray());
}
}

innerCharPos = 0;
_lineStartPos = 0;

if (flushStream && flushEncoder)
{
var accArray = _acc.ToArray();
if (accArray.Length > 0)
{
innerStream.Write(accArray.AsSpan<byte>().Slice(0, accArray.Length));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
var accArray = _acc.ToArray();
if (accArray.Length > 0)
{
innerStream.Write(accArray.AsSpan<byte>().Slice(0, accArray.Length));
var accSpan = CollectionsMarshal.AsSpan(_acc);
if (accSpan.Length > 0)
{
innerStream.Write(accSpan.Slice(0, accSpan.Length));

_acc.Clear();
}
}

if (flushStream)
{
innerStream.Flush();
}
}
}
}
19 changes: 19 additions & 0 deletions src/libraries/System.Console/tests/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@ public static async Task OutWriteAndWriteLineOverloads()
}
}

[Fact]
[PlatformSpecific(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS)]
public void TestConsoleWrite()
{
Stream s = new MemoryStream();
TextWriter w = new StreamWriter(s);
((StreamWriter)w).AutoFlush = true;
TextReader r = new StreamReader(s);
Console.SetOut(w);

Console.Write("A");
Console.Write("B");
Console.Write("C");

s.Position = 0;
string line = r.ReadToEnd();
Assert.Equal("ABC", line);
}

private static unsafe void ValidateConsoleEncoding(Encoding encoding)
{
Assert.NotNull(encoding);
Expand Down
Loading