forked from apple1417/bagpipe
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamExtensions.cs
More file actions
30 lines (27 loc) · 782 Bytes
/
StreamExtensions.cs
File metadata and controls
30 lines (27 loc) · 782 Bytes
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
using System.IO;
namespace bagpipe {
static class StreamExtensions {
public static byte[] ReadByteArray(this Stream stream, int n) {
byte[] buf = new byte[n];
int len = stream.Read(buf, 0, n);
if (len != n) {
throw new EndOfStreamException();
}
return buf;
}
public static byte ReadByteSafe(this Stream stream) {
int val = stream.ReadByte();
if (val == -1) {
throw new EndOfStreamException();
}
return (byte)val;
}
public static void SeekSafe(this Stream stream, long offset, SeekOrigin origin) {
long oldPos = stream.Position;
long newPos = stream.Seek(offset, origin);
if (newPos != oldPos + offset) {
throw new EndOfStreamException();
}
}
}
}