-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamExtensions.cs
155 lines (130 loc) · 3.32 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
using System;
using System.IO;
using System.Net;
using System.Text;
namespace mccslib
{
public static class StreamExtensions
{
#region Stream helpers
public static byte ReadByte2 (this Stream s)
{
return (byte)s.ReadByte ();
}
public static int ReadInt (this Stream s)
{
return IPAddress.HostToNetworkOrder ((int)Read (s, 4));
}
public static short ReadShort (this Stream s)
{
return IPAddress.HostToNetworkOrder ((short)Read (s, 2));
}
public static long ReadLong (this Stream s)
{
return IPAddress.HostToNetworkOrder ((long)Read (s, 8));
}
public static double ReadDouble (this Stream s)
{
return new BinaryReader (s).ReadDouble ();
}
public static float ReadFloat (this Stream s)
{
return new BinaryReader (s).ReadSingle ();
}
public static Boolean ReadBoolean (this Stream s)
{
return new BinaryReader (s).ReadBoolean ();
}
public static byte[] ReadBytes (this Stream s, int count)
{
return new BinaryReader (s).ReadBytes (count);
}
public static String ReadString8 (this Stream s)
{
short len = ReadShort (s);
byte[] buf = new byte[len];
s.Read (buf, 0, len);
return Encoding.UTF8.GetString (buf);
}
public static void WriteString8 (this Stream s, String msg)
{
WriteShort (s, (short)msg.Length);
byte[] buf = Encoding.UTF8.GetBytes (msg);
s.Write (buf, 0, buf.Length);
}
public static String ReadString16 (this Stream s)
{
short len = ReadShort (s);
byte[] buf = new byte[len * 2];
s.Read (buf, 0, len * 2);
return Encoding.BigEndianUnicode.GetString (buf);
}
public static void WriteString16 (this Stream s, String msg)
{
WriteShort (s, (short)msg.Length);
byte[] buf = Encoding.BigEndianUnicode.GetBytes (msg);
s.Write (buf, 0, buf.Length);
}
public static void WriteInt (this Stream s, int i)
{
byte[] a = BitConverter.GetBytes (IPAddress.HostToNetworkOrder (i));
s.Write (a, 0, a.Length);
}
public static void WriteLong (this Stream s, long i)
{
byte[] a = BitConverter.GetBytes (IPAddress.HostToNetworkOrder (i));
s.Write (a, 0, a.Length);
}
public static void WriteShort (this Stream s, short i)
{
byte[] a = BitConverter.GetBytes (IPAddress.HostToNetworkOrder (i));
s.Write (a, 0, a.Length);
}
public static void WriteDouble (this Stream s, double d)
{
new BinaryWriter (s).Write (d);
}
public static void WriteFloat (this Stream s, float f)
{
new BinaryWriter (s).Write (f);
}
public static void WriteBoolean (this Stream s, Boolean b)
{
new BinaryWriter (s).Write (b);
}
public static void WriteBytes (this Stream s, byte[] b)
{
new BinaryWriter (s).Write (b);
}
public static Object Read (this Stream s, int num)
{
byte[] b = new byte[num];
for (int i = 0; i < b.Length; i++) {
b [i] = (byte)s.ReadByte ();
}
switch (num) {
case 4:
return BitConverter.ToInt32 (b, 0);
case 8:
return BitConverter.ToInt64 (b, 0);
case 2:
return BitConverter.ToInt16 (b, 0);
default:
return 0;
}
}
#endregion
public static FaceOffset ReadFace (this Stream s)
{
return (FaceOffset)s.ReadByte ();
}
public static MovingObject ReadObjType (this Stream s)
{
return (MovingObject)s.ReadByte ();
}
public static MobType ReadMobType(this Stream s)
{
return (MobType)s.ReadByte();
}
}
}