-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathByteWriter.cs
More file actions
121 lines (99 loc) · 2.99 KB
/
ByteWriter.cs
File metadata and controls
121 lines (99 loc) · 2.99 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KSSHServer
{
public class ByteWriter : IDisposable
{
private MemoryStream _Stream = new MemoryStream();
public void WritePacketType(Packets.PacketType packetType)
{
WriteByte((byte)packetType);
}
public void WriteByte(byte value)
{
if (disposedValue)
throw new ObjectDisposedException("ByteWriter");
_Stream.WriteByte(value);
}
public void WriteBytes(byte[] data)
{
WriteUInt32((uint)data.Count());
WriteRawBytes(data);
}
public void WriteString(string data)
{
WriteString(data, Encoding.ASCII);
}
public void WriteString(string data, Encoding encoding)
{
WriteBytes(encoding.GetBytes(data));
}
public void WriteStringList(IEnumerable list)
{
WriteString(string.Join(",", list));
}
public void WriteUInt32(uint data)
{
byte[] buffer = BitConverter.GetBytes(data);
if (BitConverter.IsLittleEndian)
buffer = buffer.Reverse().ToArray();
WriteRawBytes(buffer);
}
public void WriteMPInt(byte[] value)
{
if ((value.Length == 1) && (value[0] == 0))
{
WriteUInt32(0);
return;
}
uint length = (uint)value.Length;
if ((value[0] & 0x80) != 0)
{
WriteUInt32((uint)length + 1);
WriteByte(0x00);
}
else
{
WriteUInt32((uint)length);
}
WriteRawBytes(value);
}
public void WriteRawBytes(byte[] value)
{
if (disposedValue)
throw new ObjectDisposedException("ByteWriter");
_Stream.Write(value, 0, value.Count());
}
public byte[] ToByteArray()
{
if (disposedValue)
throw new ObjectDisposedException("ByteWriter");
return _Stream.ToArray();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_Stream.Dispose();
_Stream = null;
}
disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
}
#endregion
}
}