-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
79 lines (65 loc) · 2.55 KB
/
Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace XexDecryptor {
public static class Util {
public static byte[] ReadEntireStream (Stream stream) {
var result = new List<byte>();
var buffer = new byte[32767];
while (true) {
var bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead < buffer.Length) {
if (bytesRead > 0) {
result.Capacity = result.Count + bytesRead;
result.AddRange(buffer.Take(bytesRead));
}
if (bytesRead <= 0)
break;
} else {
result.AddRange(buffer);
}
}
return result.ToArray();
}
public static void RunProcess (string filename, string parameters, byte[] stdin, out string stderr, out byte[] stdout) {
var psi = new ProcessStartInfo(filename, parameters);
psi.WorkingDirectory = Path.GetDirectoryName(filename);
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
using (var process = Process.Start(psi)) {
var stdinStream = process.StandardInput.BaseStream;
var stderrStream = process.StandardError.BaseStream;
if (stdin != null) {
ThreadPool.QueueUserWorkItem(
(_) => {
if (stdin != null) {
stdinStream.Write(
stdin, 0, stdin.Length
);
stdinStream.Flush();
}
stdinStream.Close();
}, null
);
}
var temp = new string[1] { null };
ThreadPool.QueueUserWorkItem(
(_) => {
var text = Encoding.ASCII.GetString(ReadEntireStream(stderrStream));
temp[0] = text;
}, null
);
stdout = ReadEntireStream(process.StandardOutput.BaseStream);
process.WaitForExit();
stderr = temp[0];
process.Close();
}
}
}
}