Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions src/libraries/System.Console/src/System/ConsolePal.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace System
Expand Down
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
55 changes: 51 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -36,6 +37,8 @@ public class StreamWriter : TextWriter
private readonly char[] _charBuffer;
private int _charPos;
private int _charLen;
private List<byte> _acc = new List<byte>();
private int _lineStartPos;
private bool _autoFlush;
private bool _haveWrittenPreamble;
private readonly bool _closable;
Expand Down Expand Up @@ -320,11 +323,55 @@ private void Flush(bool flushStream, bool flushEncoder)
(_byteBuffer = new byte[_encoding.GetMaxByteCount(_charBuffer.Length)]);
}

int count = _encoder.GetBytes(new ReadOnlySpan<char>(_charBuffer, 0, _charPos), byteBuffer, flushEncoder);
_charPos = 0;
if (count > 0)
if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS() || OperatingSystem.IsMacCatalyst())
Copy link
Member

Choose a reason for hiding this comment

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

StreamWriter is a public class that can be used for many things. This particular logic changes it's behaviour for few OSes. Could you please create a custom type that derives from StreamWriter , override Flush and move the new logic there? You might need to make some of its private fields protected internal.

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

_stream.Write(_acc.ToArray().AsSpan<byte>());
_acc.Clear();

_lineStartPos = i + 1;
}
}

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

_charPos = 0;
_lineStartPos = 0;

if (flushStream && flushEncoder)
{
var accArray = _acc.ToArray();
if (accArray.Length > 0)
{
_stream.Write(accArray.AsSpan<byte>().Slice(0, accArray.Length));
_acc.Clear();
}
}
}
else
{
int count = _encoder.GetBytes(new ReadOnlySpan<char>(_charBuffer, 0, _charPos), byteBuffer, flushEncoder);
_charPos = 0;
if (count > 0)
{
_stream.Write(byteBuffer.Slice(0, count));
}
}

if (flushStream)
Expand Down