forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePackExtendedTypeObject.cs
More file actions
310 lines (275 loc) · 8.86 KB
/
MessagePackExtendedTypeObject.cs
File metadata and controls
310 lines (275 loc) · 8.86 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2010-2018 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
#if UNITY_5 || UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WII || UNITY_IPHONE || UNITY_ANDROID || UNITY_PS3 || UNITY_XBOX360 || UNITY_FLASH || UNITY_BKACKBERRY || UNITY_WINRT
#define UNITY
#endif
using System;
using System.Text;
namespace MsgPack
{
/// <summary>
/// Represents Message Pack extended type object.
/// </summary>
public struct MessagePackExtendedTypeObject : IEquatable<MessagePackExtendedTypeObject>
{
/// <summary>
/// A type code of this object.
/// </summary>
private readonly byte _typeCode;
/// <summary>
/// Gets a type code of this object.
/// </summary>
/// <value>
/// A type code. Note that values over <see cref="SByte.MaxValue"/> are reserved for MsgPack spec itself.
/// </value>
public byte TypeCode
{
get { return this._typeCode; }
}
/// <summary>
/// A binary value portion of this object.
/// </summary>
private readonly byte[] _body;
/// <summary>
/// Gets a binary value portion of this object.
/// </summary>
/// <value>
/// A binary value portion of this object. This value will not be null.
/// </value>
#if UNITY && DEBUG
public
#else
internal
#endif
byte[] Body
{
get { return this._body ?? Binary.Empty; }
}
/// <summary>
/// Gets a copy of the binary value portion of this object.
/// </summary>
/// <value>
/// A copy of the binary value portion of this object. This value will not be null.
/// </value>
public byte[] GetBody()
{
return this._body == null ? Binary.Empty : this._body.Clone() as byte[];
}
/// <summary>
/// Gets a value indicating whether this instance is valid.
/// </summary>
/// <value>
/// <c>true</c> if this instance is valid; otherwise, <c>false</c>.
/// </value>
public bool IsValid
{
get { return this._body != null; }
}
// For unpacking
private MessagePackExtendedTypeObject( byte[] body, byte unpackedTypeCode )
{
if ( body == null )
{
throw new ArgumentNullException( "body" );
}
this._typeCode = unpackedTypeCode;
this._body = body;
}
/// <summary>
/// Initializes a new instance of the <see cref="MessagePackExtendedTypeObject"/> struct.
/// </summary>
/// <param name="typeCode">A type code of this extension object.</param>
/// <param name="body">A binary value portion.</param>
/// <exception cref="System.ArgumentException">
/// The <paramref name="typeCode"/> is over 127. Higher values are reserved for MessagePack format specification.
/// </exception>
/// <exception cref="System.ArgumentNullException">The <paramref name="body"/> is <c>null</c>.</exception>
public MessagePackExtendedTypeObject( byte typeCode, byte[] body )
{
if ( typeCode > 0x7F )
{
throw new ArgumentException(
"A typeCode must be less than 128 because higher values are reserved for MessagePack format specification.",
"typeCode" );
}
if ( body == null )
{
throw new ArgumentNullException( "body" );
}
this._typeCode = typeCode;
this._body = body;
}
/// <summary>
/// Creates a new instance of the <see cref="MessagePackExtendedTypeObject"/> struct.
/// </summary>
/// <param name="typeCode">A type code of this extension object.</param>
/// <param name="body">A binary value portion.</param>
/// <exception cref="System.ArgumentNullException">The <paramref name="body"/> is <c>null</c>.</exception>
/// <remarks>
/// This method allows reserved type code. It means that this method does not throw exception when the <paramref name="typeCode"/> is reserved value (greater then 0x7F).
/// </remarks>
public static MessagePackExtendedTypeObject Unpack( byte typeCode, byte[] body )
{
return new MessagePackExtendedTypeObject( body, typeCode );
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString()
{
if ( this._body == null )
{
return String.Empty;
}
var buffer = new StringBuilder( 7 + this._body.Length * 2 );
this.ToString( buffer, false );
return buffer.ToString();
}
#if UNITY && DEBUG
public
#else
internal
#endif
void ToString( StringBuilder buffer, bool isJson )
{
if ( isJson )
{
if ( this._body == null )
{
// Assume that JSON is used when a collection is stringified, so empty string should break entire JSON.
buffer.Append( "null" );
return;
}
buffer.Append( "{\"TypeCode\":" ).Append( this._typeCode ).Append( ", \"Body\":\"" );
Binary.ToHexString( this._body, buffer );
buffer.Append( "\"}" );
}
else
{
if ( this._body == null )
{
return;
}
buffer.Append( "[" ).Append( this._typeCode ).Append( "]" );
Binary.ToHexString( this._body, buffer );
}
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
if ( this._body == null )
{
return 0;
}
unchecked
{
int hashCode = this._typeCode << 24;
hashCode ^= this._body.Length;
int hashCodeTargetLength = Math.Min( this._body.Length / 4, 8 );
for ( int i = 0; i < hashCodeTargetLength; i++ )
{
uint temp = this._body[ i ];
temp |= ( uint )( this._body[ i + 1 ] << 8 );
temp |= ( uint )( this._body[ i + 2 ] << 16 );
temp |= ( uint )( this._body[ i + 3 ] << 24 );
hashCode ^= ( int )temp;
}
return hashCode;
}
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals( object obj )
{
if ( !( obj is MessagePackExtendedTypeObject ) )
{
return false;
}
return this.Equals( ( MessagePackExtendedTypeObject )obj );
}
/// <summary>
/// Determines whether the specified <see cref="MessagePackExtendedTypeObject" /> is equal to this instance.
/// </summary>
/// <param name="other">The <see cref="MessagePackExtendedTypeObject" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="MessagePackExtendedTypeObject" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public bool Equals( MessagePackExtendedTypeObject other )
{
if ( this._typeCode != other._typeCode )
{
return false;
}
if ( ReferenceEquals( this._body, other._body ) )
{
return true;
}
if ( this._body.Length != other._body.Length )
{
return false;
}
for ( int i = 0; i < this._body.Length; i++ )
{
if ( this._body[ i ] != other._body[ i ] )
{
return false;
}
}
return true;
}
/// <summary>
/// Determines whether the specified <see cref="MessagePackExtendedTypeObject" />s are equal.
/// </summary>
/// <param name="left">A <see cref="MessagePackExtendedTypeObject" />.</param>
/// <param name="right">A <see cref="MessagePackExtendedTypeObject" />.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="MessagePackExtendedTypeObject" />s are equal; otherwise, <c>false</c>.
/// </returns>
public static bool operator ==( MessagePackExtendedTypeObject left, MessagePackExtendedTypeObject right )
{
return left.Equals( right );
}
/// <summary>
/// Determines whether the specified <see cref="MessagePackExtendedTypeObject" />s are not equal.
/// </summary>
/// <param name="left">A <see cref="MessagePackExtendedTypeObject" />.</param>
/// <param name="right">A <see cref="MessagePackExtendedTypeObject" />.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="MessagePackExtendedTypeObject" />s are not equal; otherwise, <c>false</c>.
/// </returns>
public static bool operator !=( MessagePackExtendedTypeObject left, MessagePackExtendedTypeObject right )
{
return !left.Equals( right );
}
}
}