public static ToUInt64 ( byte buffer, int offset ) : ulong | ||
buffer | byte | |
offset | int | |
return | ulong |
public void PerformanceTest() { var dummy = new byte[8]; // Let's load and JIT BigEndianBinary.ToInt16(dummy, 0); BigEndianBinary.ToInt32(dummy, 0); BigEndianBinary.ToInt64(dummy, 0); BigEndianBinary.ToUInt16(dummy, 0); BigEndianBinary.ToUInt32(dummy, 0); BigEndianBinary.ToUInt64(dummy, 0); BigEndianBinary.ToSingle(dummy, 0); BigEndianBinary.ToDouble(dummy, 0); BitConverter.ToInt16(dummy, 0); BitConverter.ToInt32(dummy, 0); BitConverter.ToInt64(dummy, 0); BitConverter.ToUInt16(dummy, 0); BitConverter.ToUInt32(dummy, 0); BitConverter.ToUInt64(dummy, 0); BitConverter.ToSingle(dummy, 0); BitConverter.ToDouble(dummy, 0); // Go const int iteration = 1000000; PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0 }, BigEndianBinary.ToInt16, BitConverter.ToInt16, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0 }, BigEndianBinary.ToUInt16, BitConverter.ToUInt16, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0xff, 0xff, 0 }, BigEndianBinary.ToInt32, BitConverter.ToInt32, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0xff, 0xff, 0 }, BigEndianBinary.ToUInt32, BitConverter.ToUInt32, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0 }, BigEndianBinary.ToInt64, BitConverter.ToInt64, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0 }, BigEndianBinary.ToUInt64, BitConverter.ToUInt64, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0x00, 0xff, 0 }, BigEndianBinary.ToSingle, BitConverter.ToSingle, iteration); PerformanceTestCore(new byte[] { 0, 0x80, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0 }, BigEndianBinary.ToDouble, BitConverter.ToDouble, iteration); }
/// <summary> /// Decodes specified <see cref="MessagePackExtendedTypeObject"/> and returns an equivalant <see cref="Timestamp"/>. /// </summary> /// <param name="value"><see cref="MessagePackExtendedTypeObject"/> which is native representation of <see cref="Timestamp"/>.</param> /// <returns><see cref="Timestamp"/>.</returns> /// <exception cref="ArgumentException"> /// <paramref name="value"/> does not represent msgpack timestamp. Specifically, the type code is not equal to <see cref="TypeCode"/> value. /// Or, <paramref name="value"/> does not have valid msgpack timestamp structure. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="value"/> have invalid nanoseconds value. /// </exception> /// <remarks> /// A definition of valid msgpack time stamp is: /// <list type="bullet"> /// <item>Its type code is <c>0xFF</c>(<c>-1</c>).</item> /// <item>Its length is 4, 8, or 12 bytes.</item> /// <item>Its nanoseconds part is between 0 and 999,999,999.</item> /// </list> /// </remarks> public static Timestamp Decode(MessagePackExtendedTypeObject value) { if (value.TypeCode != TypeCode) { throw new ArgumentException("The value's type code must be 0xFF.", "value"); } switch (value.Body.Length) { case 4: { // timespan32 format return(new Timestamp(BigEndianBinary.ToUInt32(value.Body, 0), 0)); } case 8: { // timespan64 format var payload = BigEndianBinary.ToUInt64(value.Body, 0); return(new Timestamp(unchecked (( long )(payload & 0x00000003ffffffffL)), unchecked (( int )(payload >> 34)))); } case 12: { // timespan96 format return(new Timestamp(BigEndianBinary.ToInt64(value.Body, sizeof(int)), unchecked (( int )BigEndianBinary.ToUInt32(value.Body, 0)))); } default: { throw new ArgumentException("The value's length is not valid.", "value"); } } }
public void TestToUInt64() { AssertPrimitive("0", BigEndianBinary.ToUInt64(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }, 0)); AssertPrimitive("7fffffffffffffff", BigEndianBinary.ToUInt64(new byte[] { 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 0)); AssertPrimitive("8000000000000000", BigEndianBinary.ToUInt64(new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0)); AssertPrimitive("ffffffffffffffff", BigEndianBinary.ToUInt64(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 0)); AssertPrimitive("8000000000000000", BigEndianBinary.ToUInt64(new byte[] { 0x1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2 }, 1)); }
private ReadValueResult ReadValue(out byte header, out long integral, out float real32, out double real64) { var readHeader = this.ReadByteFromSource(); // This is BAD practice for out, but it reduces IL size very well for this method. integral = default(long); real32 = default(float); real64 = default(double); if (readHeader < 0) { header = 0; return(ReadValueResult.Eof); } header = unchecked (( byte )readHeader); switch (header >> 4) { case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7: { // PositiveFixNum this.InternalCollectionType = CollectionType.None; integral = header; return(ReadValueResult.Byte); } case 0x8: { // FixMap integral = header & 0xF; return(ReadValueResult.MapLength); } case 0x9: { // FixArray integral = header & 0xF; return(ReadValueResult.ArrayLength); } case 0xA: case 0xB: { // FixRaw integral = header & 0x1F; return(ReadValueResult.String); } case 0xE: case 0xF: { // NegativeFixNum this.InternalCollectionType = CollectionType.None; integral = header | unchecked (( long )0xFFFFFFFFFFFFFF00); return(ReadValueResult.SByte); } } switch (header) { case MessagePackCode.NilValue: { return(ReadValueResult.Nil); } case MessagePackCode.TrueValue: { integral = 1; return(ReadValueResult.Boolean); } case MessagePackCode.FalseValue: { integral = 0; return(ReadValueResult.Boolean); } case MessagePackCode.SignedInt8: { this.ReadStrict(this._scalarBuffer, sizeof(sbyte)); integral = BigEndianBinary.ToSByte(this._scalarBuffer, 0); return(ReadValueResult.SByte); } case MessagePackCode.SignedInt16: { this.ReadStrict(this._scalarBuffer, sizeof(short)); integral = BigEndianBinary.ToInt16(this._scalarBuffer, 0); return(ReadValueResult.Int16); } case MessagePackCode.SignedInt32: { this.ReadStrict(this._scalarBuffer, sizeof(int)); integral = BigEndianBinary.ToInt32(this._scalarBuffer, 0); return(ReadValueResult.Int32); } case MessagePackCode.SignedInt64: { this.ReadStrict(this._scalarBuffer, sizeof(long)); integral = BigEndianBinary.ToInt64(this._scalarBuffer, 0); return(ReadValueResult.Int64); } case MessagePackCode.UnsignedInt8: { this.ReadStrict(this._scalarBuffer, sizeof(byte)); integral = BigEndianBinary.ToByte(this._scalarBuffer, 0); return(ReadValueResult.Byte); } case MessagePackCode.UnsignedInt16: { this.ReadStrict(this._scalarBuffer, sizeof(ushort)); integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); return(ReadValueResult.UInt16); } case MessagePackCode.UnsignedInt32: { this.ReadStrict(this._scalarBuffer, sizeof(uint)); integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); return(ReadValueResult.UInt32); } case MessagePackCode.UnsignedInt64: { this.ReadStrict(this._scalarBuffer, sizeof(ulong)); integral = unchecked (( long )BigEndianBinary.ToUInt64(this._scalarBuffer, 0)); return(ReadValueResult.UInt64); } case MessagePackCode.Real32: { this.ReadStrict(this._scalarBuffer, sizeof(float)); real32 = BigEndianBinary.ToSingle(this._scalarBuffer, 0); return(ReadValueResult.Single); } case MessagePackCode.Real64: { this.ReadStrict(this._scalarBuffer, sizeof(double)); real64 = BigEndianBinary.ToDouble(this._scalarBuffer, 0); return(ReadValueResult.Double); } case MessagePackCode.Bin8: { this.ReadStrict(this._scalarBuffer, sizeof(byte)); integral = BigEndianBinary.ToByte(this._scalarBuffer, 0); return(ReadValueResult.Binary); } case MessagePackCode.Str8: { this.ReadStrict(this._scalarBuffer, sizeof(byte)); integral = BigEndianBinary.ToByte(this._scalarBuffer, 0); return(ReadValueResult.String); } case MessagePackCode.Bin16: { this.ReadStrict(this._scalarBuffer, sizeof(ushort)); integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); return(ReadValueResult.Binary); } case MessagePackCode.Raw16: { this.ReadStrict(this._scalarBuffer, sizeof(ushort)); integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); return(ReadValueResult.String); } case MessagePackCode.Bin32: { this.ReadStrict(this._scalarBuffer, sizeof(uint)); integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); return(ReadValueResult.Binary); } case MessagePackCode.Raw32: { this.ReadStrict(this._scalarBuffer, sizeof(uint)); integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); return(ReadValueResult.String); } case MessagePackCode.Array16: { this.ReadStrict(this._scalarBuffer, sizeof(ushort)); integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); return(ReadValueResult.ArrayLength); } case MessagePackCode.Array32: { this.ReadStrict(this._scalarBuffer, sizeof(uint)); integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); return(ReadValueResult.ArrayLength); } case MessagePackCode.Map16: { this.ReadStrict(this._scalarBuffer, sizeof(ushort)); integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); return(ReadValueResult.MapLength); } case MessagePackCode.Map32: { this.ReadStrict(this._scalarBuffer, sizeof(uint)); integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); return(ReadValueResult.MapLength); } case MessagePackCode.FixExt1: { return(ReadValueResult.FixExt1); } case MessagePackCode.FixExt2: { return(ReadValueResult.FixExt2); } case MessagePackCode.FixExt4: { return(ReadValueResult.FixExt4); } case MessagePackCode.FixExt8: { return(ReadValueResult.FixExt8); } case MessagePackCode.FixExt16: { return(ReadValueResult.FixExt16); } case MessagePackCode.Ext8: { return(ReadValueResult.Ext8); } case MessagePackCode.Ext16: { return(ReadValueResult.Ext16); } case MessagePackCode.Ext32: { return(ReadValueResult.Ext32); } default: { this.ThrowUnassignedMessageTypeException(readHeader); // Never reach return(ReadValueResult.Eof); } } }
private async Task <AsyncReadValueResult> ReadValueAsync(CancellationToken cancellationToken) { var readHeader = await this.ReadByteFromSourceAsync(cancellationToken).ConfigureAwait(false); var result = default(AsyncReadValueResult); if (readHeader < 0) { return(result); } var header = unchecked (( byte )readHeader); result.header = header; switch (header >> 4) { case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7: { // PositiveFixNum this.InternalCollectionType = CollectionType.None; result.integral = header; result.type = ReadValueResult.Byte; return(result); } case 0x8: { // FixMap result.integral = header & 0xF; result.type = ReadValueResult.MapLength; return(result); } case 0x9: { // FixArray result.integral = header & 0xF; result.type = ReadValueResult.ArrayLength; return(result); } case 0xA: case 0xB: { // FixRaw result.integral = header & 0x1F; result.type = ReadValueResult.String; return(result); } case 0xE: case 0xF: { // NegativeFixNum this.InternalCollectionType = CollectionType.None; result.integral = header | unchecked (( long )0xFFFFFFFFFFFFFF00); result.type = ReadValueResult.SByte; return(result); } } switch (header) { case MessagePackCode.NilValue: { result.type = ReadValueResult.Nil; return(result); } case MessagePackCode.TrueValue: { result.integral = 1; result.type = ReadValueResult.Boolean; return(result); } case MessagePackCode.FalseValue: { result.integral = 0; result.type = ReadValueResult.Boolean; return(result); } case MessagePackCode.SignedInt8: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(sbyte), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToSByte(this._scalarBuffer, 0); result.type = ReadValueResult.SByte; return(result); } case MessagePackCode.SignedInt16: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(short), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToInt16(this._scalarBuffer, 0); result.type = ReadValueResult.Int16; return(result); } case MessagePackCode.SignedInt32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(int), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToInt32(this._scalarBuffer, 0); result.type = ReadValueResult.Int32; return(result); } case MessagePackCode.SignedInt64: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(long), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToInt64(this._scalarBuffer, 0); result.type = ReadValueResult.Int64; return(result); } case MessagePackCode.UnsignedInt8: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToByte(this._scalarBuffer, 0); result.type = ReadValueResult.Byte; return(result); } case MessagePackCode.UnsignedInt16: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); result.type = ReadValueResult.UInt16; return(result); } case MessagePackCode.UnsignedInt32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); result.type = ReadValueResult.UInt32; return(result); } case MessagePackCode.UnsignedInt64: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(ulong), cancellationToken).ConfigureAwait(false); result.integral = unchecked (( long )BigEndianBinary.ToUInt64(this._scalarBuffer, 0)); result.type = ReadValueResult.UInt64; return(result); } case MessagePackCode.Real32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(float), cancellationToken).ConfigureAwait(false); result.real32 = BigEndianBinary.ToSingle(this._scalarBuffer, 0); result.type = ReadValueResult.Single; return(result); } case MessagePackCode.Real64: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(double), cancellationToken).ConfigureAwait(false); result.real64 = BigEndianBinary.ToDouble(this._scalarBuffer, 0); result.type = ReadValueResult.Double; return(result); } case MessagePackCode.Bin8: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToByte(this._scalarBuffer, 0); result.type = ReadValueResult.Binary; return(result); } case MessagePackCode.Str8: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToByte(this._scalarBuffer, 0); result.type = ReadValueResult.String; return(result); } case MessagePackCode.Bin16: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); result.type = ReadValueResult.Binary; return(result); } case MessagePackCode.Raw16: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); result.type = ReadValueResult.String; return(result); } case MessagePackCode.Bin32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); result.type = ReadValueResult.Binary; return(result); } case MessagePackCode.Raw32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); result.type = ReadValueResult.String; return(result); } case MessagePackCode.Array16: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); result.type = ReadValueResult.ArrayLength; return(result); } case MessagePackCode.Array32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); result.type = ReadValueResult.ArrayLength; return(result); } case MessagePackCode.Map16: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt16(this._scalarBuffer, 0); result.type = ReadValueResult.MapLength; return(result); } case MessagePackCode.Map32: { await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false); result.integral = BigEndianBinary.ToUInt32(this._scalarBuffer, 0); result.type = ReadValueResult.MapLength; return(result); } case MessagePackCode.FixExt1: { result.type = ReadValueResult.FixExt1; return(result); } case MessagePackCode.FixExt2: { result.type = ReadValueResult.FixExt2; return(result); } case MessagePackCode.FixExt4: { result.type = ReadValueResult.FixExt4; return(result); } case MessagePackCode.FixExt8: { result.type = ReadValueResult.FixExt8; return(result); } case MessagePackCode.FixExt16: { result.type = ReadValueResult.FixExt16; return(result); } case MessagePackCode.Ext8: { result.type = ReadValueResult.Ext8; return(result); } case MessagePackCode.Ext16: { result.type = ReadValueResult.Ext16; return(result); } case MessagePackCode.Ext32: { result.type = ReadValueResult.Ext32; return(result); } default: { this.ThrowUnassignedMessageTypeException(readHeader); // Never reach result.type = ReadValueResult.Eof; return(result); } } }