-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use caching ConsoleStream for both iOS and Android (#58967)
Expands on #56713 by moving the caching implementation to a separate internal class leaving only the interop calls in ConsolePal.iOS and ConsolePal.Android Fixes #57304
- Loading branch information
Showing
4 changed files
with
89 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/libraries/System.Console/src/System/IO/CachedConsoleStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace System.IO | ||
{ | ||
internal abstract class CachedConsoleStream : ConsoleStream | ||
{ | ||
private readonly StringBuilder _buffer = new StringBuilder(); | ||
private readonly Encoding _encoding; | ||
private readonly Decoder _decoder; | ||
|
||
public CachedConsoleStream(Encoding encoding) : base(FileAccess.Write) | ||
{ | ||
_encoding = encoding; | ||
_decoder = _encoding.GetDecoder(); | ||
} | ||
|
||
public override int Read(Span<byte> buffer) => throw Error.GetReadNotSupported(); | ||
|
||
public override void Write(ReadOnlySpan<byte> buffer) | ||
{ | ||
int maxCharCount = _encoding.GetMaxCharCount(buffer.Length); | ||
char[]? pooledBuffer = null; | ||
Span<char> charSpan = maxCharCount <= 512 ? stackalloc char[512] : (pooledBuffer = ArrayPool<char>.Shared.Rent(maxCharCount)); | ||
try | ||
{ | ||
int count = _decoder.GetChars(buffer, charSpan, false); | ||
if (count > 0) | ||
{ | ||
WriteOrCache(this, _buffer, charSpan.Slice(0, count)); | ||
} | ||
} | ||
finally | ||
{ | ||
if (pooledBuffer != null) | ||
{ | ||
ArrayPool<char>.Shared.Return(pooledBuffer); | ||
} | ||
} | ||
} | ||
|
||
protected abstract void Print(ReadOnlySpan<char> line); | ||
|
||
private static void WriteOrCache(CachedConsoleStream stream, StringBuilder cache, Span<char> charBuffer) | ||
{ | ||
int lastNewLine = charBuffer.LastIndexOf('\n'); | ||
if (lastNewLine != -1) | ||
{ | ||
Span<char> lineSpan = charBuffer.Slice(0, lastNewLine); | ||
if (cache.Length > 0) | ||
{ | ||
stream.Print(cache.Append(lineSpan).ToString()); | ||
cache.Clear(); | ||
} | ||
else | ||
{ | ||
stream.Print(lineSpan); | ||
} | ||
|
||
if (lastNewLine + 1 < charBuffer.Length) | ||
{ | ||
cache.Append(charBuffer.Slice(lastNewLine + 1)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
// no newlines found, add the entire buffer to the cache | ||
cache.Append(charBuffer); | ||
} | ||
} | ||
} |