-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathStreamExtensions.cs
196 lines (173 loc) · 7.27 KB
/
StreamExtensions.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.IO;
using System.Threading.Tasks;
namespace Waher.Runtime.IO
{
/// <summary>
/// Static class that extends the <see cref="Stream"/> class with secured buffer operations.
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read, in which case an <see cref="IOException"/> is thrown.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="Buffer">Buffer to read into.</param>
/// <param name="Pos">Position in buffer to read to.</param>
/// <param name="Count">Number of bytes to read.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="Count"/> or <paramref name="Pos"/> are negative,
/// or go beyond the range of the buffer.</exception>
/// <exception cref="IOException">If not sufficient bytes exist to be read.</exception>
public static void ReadAll(this Stream f, byte[] Buffer, int Pos, int Count)
{
if (TryReadAll(f, Buffer, Pos, Count) != Count)
throw new IOException("Unexpected end of stream.");
}
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="Buffer">Buffer to read into.</param>
/// <param name="Pos">Position in buffer to read to.</param>
/// <param name="Count">Number of bytes to read.</param>
/// <returns>The number of bytes reached.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="Count"/> or <paramref name="Pos"/> are negative,
/// or go beyond the range of the buffer.</exception>
public static int TryReadAll(this Stream f, byte[] Buffer, int Pos, int Count)
{
if (Count < 0)
throw new ArgumentOutOfRangeException(nameof(Count));
if (Pos < 0)
throw new ArgumentOutOfRangeException(nameof(Count));
if (Pos + Count > Buffer.Length)
throw new ArgumentOutOfRangeException(nameof(Count));
int c = 0;
while (Count > 0)
{
int i = f.Read(Buffer, Pos, Count);
if (i <= 0)
return c;
Pos += i;
Count -= i;
c += i;
}
return c;
}
/// <summary>
/// Reads all available data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read, in which case an <see cref="IOException"/> is thrown.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <exception cref="IOException">If not sufficient bytes exist to be read.</exception>
/// <exception cref="InternalBufferOverflowException">If more data is available than can be read
/// into a byte buffer.</exception>
public static async Task<byte[]> ReadAllAsync(this Stream f)
{
long Len = f.Length;
long Left = Len - f.Position;
if (Left > int.MaxValue)
{
if (f is FileStream fs)
throw new InternalBufferOverflowException("File too large: " + fs.Name);
else
throw new InternalBufferOverflowException("Stream too large.");
}
int Count = (int)Left;
byte[] Buffer = new byte[Count];
await f.ReadAllAsync(Buffer, 0, Count);
return Buffer;
}
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read, in which case an <see cref="IOException"/> is thrown.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="Buffer">Buffer to read into.</param>
/// <exception cref="IOException">If not sufficient bytes exist to be read.</exception>
public static Task ReadAllAsync(this Stream f, byte[] Buffer)
{
return f.ReadAllAsync(Buffer, 0, Buffer.Length);
}
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read, in which case an <see cref="IOException"/> is thrown.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="NrBytes">Number of bytes to read.</param>
/// <exception cref="IOException">If not sufficient bytes exist to be read.</exception>
public static async Task<byte[]> ReadAllAsync(this Stream f, int NrBytes)
{
byte[] Buffer = new byte[NrBytes];
await f.ReadAllAsync(Buffer, 0, NrBytes);
return Buffer;
}
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read, in which case an <see cref="IOException"/> is thrown.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="Buffer">Buffer to read into.</param>
/// <param name="Pos">Position in buffer to read to.</param>
/// <param name="Count">Number of bytes to read.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="Count"/> or <paramref name="Pos"/> are negative,
/// or go beyond the range of the buffer.</exception>
/// <exception cref="IOException">If not sufficient bytes exist to be read.</exception>
public static async Task ReadAllAsync(this Stream f, byte[] Buffer, int Pos, int Count)
{
if (await TryReadAllAsync(f, Buffer, Pos, Count) != Count)
throw new IOException("Unexpected end of stream.");
}
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="Buffer">Buffer to read into.</param>
/// <returns>The number of bytes reached.</returns>
public static Task<int> TryReadAllAsync(this Stream f, byte[] Buffer)
{
return f.TryReadAllAsync(Buffer, 0, Buffer.Length);
}
/// <summary>
/// Reads data from a stream to a buffer. Multiple read operations are performed until
/// either all requested bytes have been read, or no more bytes are available to be
/// read.
/// </summary>
/// <param name="f">Stream to read from.</param>
/// <param name="Buffer">Buffer to read into.</param>
/// <param name="Pos">Position in buffer to read to.</param>
/// <param name="Count">Number of bytes to read.</param>
/// <returns>The number of bytes reached.</returns>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="Count"/> or <paramref name="Pos"/> are negative,
/// or go beyond the range of the buffer.</exception>
public static async Task<int> TryReadAllAsync(this Stream f, byte[] Buffer, int Pos, int Count)
{
if (Count < 0)
throw new ArgumentOutOfRangeException(nameof(Count));
if (Pos < 0)
throw new ArgumentOutOfRangeException(nameof(Count));
if (Pos + Count > Buffer.Length)
throw new ArgumentOutOfRangeException(nameof(Count));
int c = 0;
while (Count > 0)
{
int i = await f.ReadAsync(Buffer, Pos, Count);
if (i <= 0)
return c;
Pos += i;
Count -= i;
c += i;
}
return c;
}
}
}