-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented an ExeHelper.Execute overload with ability to redir…
…ect standard output and error streams to the specified writers.
- Loading branch information
1 parent
7274c8f
commit 8d33d22
Showing
3 changed files
with
206 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// <copyright file="StreamRedirector.cs" company="Heleonix - Hennadii Lutsyshyn"> | ||
// Copyright (c) Heleonix - Hennadii Lutsyshyn. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the repository root for full license information. | ||
// </copyright> | ||
|
||
namespace Heleonix.Execution; | ||
|
||
/// <summary> | ||
/// Forwards data from a source stream into a destination stream using text writers. | ||
/// </summary> | ||
internal class StreamRedirector | ||
{ | ||
private readonly TextReader source; | ||
|
||
private readonly TextWriter destination; | ||
|
||
private readonly int bufferSize; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="StreamRedirector"/> class. | ||
/// </summary> | ||
/// <param name="source">The stream to forward data from.</param> | ||
/// <param name="destination">The stream to forward data to.</param> | ||
/// <param name="bufferSize">The size of the buffer of <see cref="char"/> data to transfer read from the | ||
/// <paramref name="source"/> and write to the <paramref name="destination"/> at once.</param> | ||
public StreamRedirector(TextReader source, TextWriter destination, int bufferSize) | ||
{ | ||
this.source = source; | ||
this.destination = destination; | ||
this.bufferSize = bufferSize; | ||
} | ||
|
||
/// <summary> | ||
/// Starts forwarding of the assigned stream on the background asynchronously. | ||
/// </summary> | ||
public void Start() | ||
{ | ||
Task.Run(async () => | ||
{ | ||
var buffer = new char[this.bufferSize]; | ||
|
||
while (true) | ||
{ | ||
var count = await this.source.ReadAsync(buffer, 0, this.bufferSize); | ||
|
||
if (count <= 0) | ||
{ | ||
break; | ||
} | ||
|
||
await this.destination.WriteAsync(buffer, 0, count); | ||
await this.destination.FlushAsync(); | ||
} | ||
}); | ||
} | ||
} |
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