-
Notifications
You must be signed in to change notification settings - Fork 2k
/
SegmentBuilder.cs
198 lines (183 loc) · 7.13 KB
/
SegmentBuilder.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Runtime.InteropServices;
namespace Orleans.Providers.Streams.Common
{
/// <summary>
/// Utility class for encoding data into an ArraySegment.
/// </summary>
public static class SegmentBuilder
{
/// <summary>
/// Calculates how much space will be needed to append the provided bytes into the segment.
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static int CalculateAppendSize(byte[] bytes)
{
return (bytes == null || bytes.Length == 0)
? sizeof(int)
: bytes.Length + sizeof(int);
}
/// <summary>
/// Calculates how much space will be needed to append the provided bytes into the segment.
/// </summary>
public static int CalculateAppendSize(ReadOnlyMemory<byte> memory)
{
return (memory.Length == 0)
? sizeof(int)
: memory.Length + sizeof(int);
}
/// <summary>
/// Calculates how much space will be needed to append the provided bytes into the segment.
/// </summary>
public static int CalculateAppendSize(ArraySegment<byte> segment)
{
return (segment.Count == 0)
? sizeof(int)
: segment.Count + sizeof(int);
}
/// <summary>
/// Calculates how much space will be needed to append the provided string into the segment.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int CalculateAppendSize(string str)
{
return (string.IsNullOrEmpty(str))
? sizeof(int)
: str.Length * sizeof(char) + sizeof(int);
}
/// <summary>
/// Appends a <see cref="ReadOnlyMemory{T}"/> of bytes to the end of the segment
/// </summary>
/// <param name="writerOffset"></param>
/// <param name="bytes"></param>
/// <param name="segment"></param>
public static void Append(ArraySegment<byte> segment, ref int writerOffset, ReadOnlyMemory<byte> bytes)
{
if (segment.Array == null)
{
throw new ArgumentNullException(nameof(segment));
}
var length = bytes.Length;
MemoryMarshal.Write(segment.AsSpan(writerOffset), ref length);
writerOffset += sizeof(int);
if (bytes.Length > 0)
{
bytes.CopyTo(segment.AsMemory(writerOffset));
writerOffset += bytes.Length;
}
}
/// <summary>
/// Appends an array of bytes to the end of the segment
/// </summary>
/// <param name="writerOffset"></param>
/// <param name="bytes"></param>
/// <param name="segment"></param>
public static void Append(ArraySegment<byte> segment, ref int writerOffset, byte[] bytes)
{
if (segment.Array == null)
{
throw new ArgumentNullException(nameof(segment));
}
if (bytes == null)
{
throw new ArgumentNullException(nameof(bytes));
}
Array.Copy(BitConverter.GetBytes(bytes.Length), 0, segment.Array, segment.Offset + writerOffset, sizeof(int));
writerOffset += sizeof(int);
if (bytes.Length != 0)
{
Array.Copy(bytes, 0, segment.Array, segment.Offset + writerOffset, bytes.Length);
writerOffset += bytes.Length;
}
}
/// <summary>
/// Appends an array of bytes to the end of the segment
/// </summary>
public static void Append(ArraySegment<byte> segment, ref int writerOffset, ArraySegment<byte> append)
{
if (segment.Array == null)
{
throw new ArgumentNullException(nameof(segment));
}
Array.Copy(BitConverter.GetBytes(append.Count), 0, segment.Array, segment.Offset + writerOffset, sizeof(int));
writerOffset += sizeof(int);
if (append.Count != 0)
{
Array.Copy(append.Array, append.Offset, segment.Array, segment.Offset + writerOffset, append.Count);
writerOffset += append.Count;
}
}
/// <summary>
/// Appends a string to the end of the segment
/// </summary>
/// <param name="writerOffset"></param>
/// <param name="str"></param>
/// <param name="segment"></param>
public static void Append(ArraySegment<byte> segment, ref int writerOffset, string str)
{
if (segment.Array == null)
{
throw new ArgumentNullException(nameof(segment));
}
if (str == null)
{
Array.Copy(BitConverter.GetBytes(-1), 0, segment.Array, segment.Offset + writerOffset, sizeof(int));
writerOffset += sizeof(int);
}
else if (string.IsNullOrEmpty(str))
{
Array.Copy(BitConverter.GetBytes(0), 0, segment.Array, segment.Offset + writerOffset, sizeof(int));
writerOffset += sizeof(int);
}
else
{
var bytes = new byte[str.Length * sizeof(char)];
Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
Append(segment, ref writerOffset, bytes);
}
}
/// <summary>
/// Reads the next item in the segment as a byte array. For performance, this is returned as a sub-segment of the original segment.
/// </summary>
/// <returns></returns>
public static ArraySegment<byte> ReadNextBytes(ArraySegment<byte> segment, ref int readerOffset)
{
if (segment.Array == null)
{
throw new ArgumentNullException(nameof(segment));
}
int size = BitConverter.ToInt32(segment.Array, segment.Offset + readerOffset);
readerOffset += sizeof(int);
var seg = new ArraySegment<byte>(segment.Array, segment.Offset + readerOffset, size);
readerOffset += size;
return seg;
}
/// <summary>
/// Reads the next item in the segment as a string.
/// </summary>
/// <returns></returns>
public static string ReadNextString(ArraySegment<byte> segment, ref int readerOffset)
{
if (segment.Array == null)
{
throw new ArgumentNullException(nameof(segment));
}
int size = BitConverter.ToInt32(segment.Array, segment.Offset + readerOffset);
readerOffset += sizeof(int);
if (size < 0)
{
return null;
}
if (size == 0)
{
return string.Empty;
}
var chars = new char[size / sizeof(char)];
Buffer.BlockCopy(segment.Array, segment.Offset + readerOffset, chars, 0, size);
readerOffset += size;
return new string(chars);
}
}
}