Repository navigation
Expand file tree
/
Copy pathStreamHelper.cs
More file actions
67 lines (52 loc) · 1.87 KB
/
Copy pathStreamHelper.cs
File metadata and controls
67 lines (52 loc) · 1.87 KB
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
using System.Buffers;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Text;
namespace CryRcon;
public static class StreamHelper
{
delegate T ReadOnlySpanFunc<TArg, T>(ReadOnlySpan<TArg> span);
static int GetByteCount(this string s)
=> Encoding.ASCII.GetByteCount(s);
static T ReadPrimitive<T>(this Stream s, ReadOnlySpanFunc<byte, T> func)
{
Span<byte> buf = stackalloc byte[Unsafe.SizeOf<T>()];
s.ReadExactly(buf);
return func(buf);
}
static void WritePrimitive<T>(this Stream s, T value, SpanAction<byte, T> func)
{
Span<byte> buf = stackalloc byte[Unsafe.SizeOf<T>()];
func(buf, value);
s.Write(buf);
}
public static byte[] ReadUInt8Array(this Stream s, int size)
{
var buf = new byte[size];
s.ReadExactly(buf);
return buf;
}
public static byte ReadUInt8(this Stream s)
{
var b = s.ReadByte();
if (b == -1)
throw new EndOfStreamException();
return (byte)b;
}
public static uint ReadUInt32BE(this Stream s)
=> s.ReadPrimitive(BinaryPrimitives.ReadUInt32BigEndian);
public static int ReadInt32LE(this Stream s)
=> s.ReadPrimitive(BinaryPrimitives.ReadInt32LittleEndian);
public static uint ReadUInt32LE(this Stream s)
=> s.ReadPrimitive(BinaryPrimitives.ReadUInt32LittleEndian);
public static void WriteUInt32BE(this Stream s, uint value)
=> s.WritePrimitive(value, BinaryPrimitives.WriteUInt32BigEndian);
public static void WriteUInt8(this Stream s, byte value)
=> s.WriteByte(value);
public static void WriteString(this Stream s, string value, int size = 256)
{
var buf = new byte[Math.Max(size, value.GetByteCount())];
Encoding.ASCII.GetBytes(value, buf);
s.Write(buf, 0, buf.Length);
}
}