-
Notifications
You must be signed in to change notification settings - Fork 232
/
BigEndianBinaryWriter.cs
174 lines (148 loc) · 4.69 KB
/
BigEndianBinaryWriter.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
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Text;
namespace KafkaNet.Common
{
/// <summary>
/// A BinaryWriter that stores values in BigEndian format.
/// </summary>
/// <remarks>
/// Booleans, bytes and byte arrays will be written directly.
/// All other values will be converted to a byte array in BigEndian byte order and written.
/// Characters and Strings will all be encoded in UTF-8 (which is byte order independent).
/// </remarks>
/// <remarks>
/// BigEndianBinaryWriter code provided by Zoltu
/// https://github.com/Zoltu/Zoltu.EndianAwareBinaryReaderWriter
/// The code was modified to implement Kafka specific byte handling.
/// </remarks>
public class BigEndianBinaryWriter : BinaryWriter
{
public BigEndianBinaryWriter(Stream stream)
: base(stream, Encoding.UTF8)
{
Contract.Requires(stream != null);
}
public BigEndianBinaryWriter(Stream stream, Boolean leaveOpen)
: base(stream, Encoding.UTF8, leaveOpen)
{
Contract.Requires(stream != null);
}
public override void Write(Decimal value)
{
var ints = Decimal.GetBits(value);
Contract.Assume(ints != null);
Contract.Assume(ints.Length == 4);
if (BitConverter.IsLittleEndian)
Array.Reverse(ints);
for (var i = 0; i < 4; ++i)
{
var bytes = BitConverter.GetBytes(ints[i]);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
Write(bytes);
}
}
public override void Write(Single value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(Double value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(Int16 value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(Int32 value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(Int64 value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(UInt16 value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(UInt32 value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(UInt64 value)
{
var bytes = BitConverter.GetBytes(value);
WriteBigEndian(bytes);
}
public override void Write(string value)
{
throw new NotSupportedException("Kafka requires specific string length prefix encoding.");
}
public void Write(byte[] value, StringPrefixEncoding encoding)
{
if (value == null)
{
Write(-1);
return;
}
switch (encoding)
{
case StringPrefixEncoding.Int16:
Write((Int16)value.Length);
break;
case StringPrefixEncoding.Int32:
Write(value.Length);
break;
}
Write(value);
}
public void Write(string value, StringPrefixEncoding encoding)
{
if (value == null)
{
switch (encoding)
{
case StringPrefixEncoding.Int16:
Write((Int16)(-1));
return;
default:
Write(-1);
return;
}
}
switch (encoding)
{
case StringPrefixEncoding.Int16:
Write((Int16)value.Length);
break;
case StringPrefixEncoding.Int32:
Write(value.Length);
break;
}
Write(Encoding.UTF8.GetBytes(value));
}
private void WriteBigEndian(Byte[] bytes)
{
Contract.Requires(bytes != null);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
Write(bytes);
}
}
public enum StringPrefixEncoding
{
Int16,
Int32,
None
};
}