Skip to content

Commit 87b2559

Browse files
committed
Changed stream reading methods to be static extension methods of the StreamReader class
1 parent 242f138 commit 87b2559

File tree

3 files changed

+121
-99
lines changed

3 files changed

+121
-99
lines changed

src/WGet.NET/Components/Internal/ProcessManager.cs

Lines changed: 4 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
// Created by basicx-StrgV //
33
// https://github.com/basicx-StrgV/ //
44
//--------------------------------------------------//
5-
using System;
6-
using System.IO;
5+
using System.Diagnostics;
76
using System.Text;
87
using System.Threading;
9-
using System.Diagnostics;
108
using System.Threading.Tasks;
11-
using WGetNET.Models;
129
using WGetNET.Extensions;
10+
using WGetNET.Models;
1311

1412
namespace WGetNET.Components.Internal
1513
{
@@ -114,7 +112,7 @@ private ProcessResult RunProcess(ProcessStartInfo processStartInfo)
114112
{
115113
proc.Start();
116114

117-
result.Output = ReadSreamOutput(proc.StandardOutput);
115+
result.Output = proc.StandardOutput.ReadSreamOutputByLine();
118116

119117
//Wait till end and get exit code
120118
proc.WaitForExit();
@@ -153,7 +151,7 @@ private async Task<ProcessResult> RunProcessAsync(ProcessStartInfo processStartI
153151
{
154152
proc.Start();
155153

156-
result.Output = await ReadSreamOutputAsync(proc.StandardOutput, cancellationToken);
154+
result.Output = await proc.StandardOutput.ReadSreamOutputByLineAsync(cancellationToken);
157155

158156
// Kill the process and return, if the task is canceled
159157
if (cancellationToken.IsCancellationRequested && !proc.HasExited)
@@ -179,71 +177,5 @@ private async Task<ProcessResult> RunProcessAsync(ProcessStartInfo processStartI
179177

180178
return result;
181179
}
182-
183-
/// <summary>
184-
/// Reads the data from the process output to a string array.
185-
/// </summary>
186-
/// <param name="output">
187-
/// The <see cref="StreamReader"/> with the process output.
188-
/// </param>
189-
/// <returns>
190-
/// A <see cref="string"/> array
191-
/// containing the process output stream content by lines.
192-
/// </returns>
193-
private string[] ReadSreamOutput(StreamReader output)
194-
{
195-
string[] outputArray = Array.Empty<string>();
196-
197-
//Read output to list
198-
while (!output.EndOfStream)
199-
{
200-
string? outputLine = output.ReadLine();
201-
if (outputLine is null)
202-
{
203-
continue;
204-
}
205-
206-
outputArray = outputArray.Add(outputLine);
207-
}
208-
209-
return outputArray;
210-
}
211-
212-
/// <summary>
213-
/// Asynchronous reads the data from the process output to a string array.
214-
/// </summary>
215-
/// <param name="output">
216-
/// The <see cref="System.IO.StreamReader"/> with the process output.
217-
/// </param>
218-
/// <param name="cancellationToken">
219-
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
220-
/// </param>
221-
/// <returns>
222-
/// A <see cref="string"/> array
223-
/// containing the process output stream content by lines.
224-
/// </returns>
225-
private async Task<string[]> ReadSreamOutputAsync(StreamReader output, CancellationToken cancellationToken = default)
226-
{
227-
string[] outputArray = Array.Empty<string>();
228-
229-
//Read output to list
230-
while (!output.EndOfStream)
231-
{
232-
if (cancellationToken.IsCancellationRequested)
233-
{
234-
break;
235-
}
236-
237-
string? outputLine = await output.ReadLineAsync();
238-
if (outputLine is null)
239-
{
240-
continue;
241-
}
242-
243-
outputArray = outputArray.Add(outputLine);
244-
}
245-
246-
return outputArray;
247-
}
248180
}
249181
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
using System;
6+
using System.IO;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace WGetNET.Extensions
11+
{
12+
/// <summary>
13+
/// The <see langword="static"/> <see cref="StreamReaderExtensions"/> class,
14+
/// provieds extension methods for the <see cref="System.IO.StringReader"/>.
15+
/// </summary>
16+
internal static class StreamReaderExtensions
17+
{
18+
/// <summary>
19+
/// Reads the data from the process output to a string array.
20+
/// </summary>
21+
/// <param name="output">
22+
/// The <see cref="StreamReader"/> with the process output.
23+
/// </param>
24+
/// <returns>
25+
/// A <see cref="string"/> array
26+
/// containing the process output stream content by lines.
27+
/// </returns>
28+
public static string[] ReadSreamOutputByLine(this StreamReader output)
29+
{
30+
string[] outputArray = Array.Empty<string>();
31+
32+
//Read output to list
33+
while (!output.EndOfStream)
34+
{
35+
string? outputLine = output.ReadLine();
36+
if (outputLine is null)
37+
{
38+
continue;
39+
}
40+
41+
outputArray = outputArray.Add(outputLine);
42+
}
43+
44+
return outputArray;
45+
}
46+
47+
/// <summary>
48+
/// Asynchronous reads the data from the process output to a string array.
49+
/// </summary>
50+
/// <param name="output">
51+
/// The <see cref="System.IO.StreamReader"/> with the process output.
52+
/// </param>
53+
/// <param name="cancellationToken">
54+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
55+
/// </param>
56+
/// <returns>
57+
/// A <see cref="string"/> array
58+
/// containing the process output stream content by lines.
59+
/// </returns>
60+
public static async Task<string[]> ReadSreamOutputByLineAsync(this StreamReader output, CancellationToken cancellationToken = default)
61+
{
62+
string[] outputArray = Array.Empty<string>();
63+
64+
//Read output to list
65+
while (!output.EndOfStream)
66+
{
67+
if (cancellationToken.IsCancellationRequested)
68+
{
69+
break;
70+
}
71+
72+
string? outputLine = await output.ReadLineAsync();
73+
if (outputLine is null)
74+
{
75+
continue;
76+
}
77+
78+
outputArray = outputArray.Add(outputLine);
79+
}
80+
81+
return outputArray;
82+
}
83+
}
84+
}

src/WGet.NET/XmlDocumentation/WGet.NET.xml

Lines changed: 33 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)