ToUInt16() public static method

public static ToUInt16 ( byte buffer, int offset ) : ushort
buffer byte
offset int
return ushort
Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public void TestToUInt16()
 {
     AssertPrimitive("0", BigEndianBinary.ToUInt16(new byte[] { 0x0, 0x0 }, 0));
     AssertPrimitive("7fff", BigEndianBinary.ToUInt16(new byte[] { 0x7f, 0xff }, 0));
     AssertPrimitive("8000", BigEndianBinary.ToUInt16(new byte[] { 0x80, 0x00 }, 0));
     AssertPrimitive("ffff", BigEndianBinary.ToUInt16(new byte[] { 0xff, 0xff }, 0));
     AssertPrimitive("8000", BigEndianBinary.ToUInt16(new byte[] { 0x1, 0x80, 0x00, 0x2 }, 1));
 }
        protected sealed override long?SkipCore()
        {
            var source = this._stream;
            var buffer = this._scalarBuffer;

#if !UNITY
            Contract.Assert(source != null, "source != null");
            Contract.Assert(buffer != null, "buffer != null");
#endif // !UNITY

            long       remainingItems       = -1;
            long       skipped              = 0;
            Int64Stack remainingCollections = null;
            do
            {
                var header = source.ReadByte();
                if (header < 0)
                {
                    return(null);
                }

                switch (header)
                {
                case MessagePackCode.NilValue:
                case MessagePackCode.TrueValue:
                case MessagePackCode.FalseValue:
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    continue;
                }
                }

                if (header < 0x80)
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    continue;
                }
                else if (header >= 0xE0)
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    continue;
                }

                switch (header & 0xF0)
                {
                case 0x80:
                {
                    var size = header & 0xF;
                    skipped += 1;
                    if (size == 0)
                    {
                        #region TryPopContextCollection

                        remainingItems--;

                        if (remainingCollections != null)
                        {
                            while (remainingItems == 0 && remainingCollections.Count > 0)
                            {
                                if (remainingCollections.Count == 0)
                                {
                                    break;
                                }

                                remainingItems = remainingCollections.Pop();
                                remainingItems--;
                            }
                        }

                        #endregion TryPopContextCollection
                    }
                    else
                    {
                        #region PushContextCollection

                        if (remainingItems >= 0)
                        {
                            if (remainingCollections == null)
                            {
                                remainingCollections = new Int64Stack(4);
                            }

                            remainingCollections.Push(remainingItems);
                        }

                        remainingItems = size * 2;

                        #endregion PushContextCollection
                    }

                    continue;
                }

                case 0x90:
                {
                    var size = header & 0xF;
                    skipped += 1;
                    if (size == 0)
                    {
                        #region TryPopContextCollection

                        remainingItems--;

                        if (remainingCollections != null)
                        {
                            while (remainingItems == 0 && remainingCollections.Count > 0)
                            {
                                if (remainingCollections.Count == 0)
                                {
                                    break;
                                }

                                remainingItems = remainingCollections.Pop();
                                remainingItems--;
                            }
                        }

                        #endregion TryPopContextCollection
                    }
                    else
                    {
                        #region PushContextCollection

                        if (remainingItems >= 0)
                        {
                            if (remainingCollections == null)
                            {
                                remainingCollections = new Int64Stack(4);
                            }

                            remainingCollections.Push(remainingItems);
                        }

                        remainingItems = size;

                        #endregion PushContextCollection
                    }

                    continue;
                }

                case 0xA0:
                case 0xB0:
                {
                    var size = header & 0x1F;
                    skipped += 1;
                    #region DrainValue

                    long bytesRead = 0;
                    while (size > bytesRead)
                    {
                        var remaining = (size - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    continue;
                }
                }

                switch (header)
                {
                case MessagePackCode.SignedInt8:
                case MessagePackCode.UnsignedInt8:
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    #region DrainValue

                    long bytesRead = 0;
                    while (1 > bytesRead)
                    {
                        var remaining = (1 - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    continue;
                }

                case MessagePackCode.SignedInt16:
                case MessagePackCode.UnsignedInt16:
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    #region DrainValue

                    long bytesRead = 0;
                    while (2 > bytesRead)
                    {
                        var remaining = (2 - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    continue;
                }

                case MessagePackCode.SignedInt32:
                case MessagePackCode.UnsignedInt32:
                case MessagePackCode.Real32:
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    #region DrainValue

                    long bytesRead = 0;
                    while (4 > bytesRead)
                    {
                        var remaining = (4 - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    continue;
                }

                case MessagePackCode.SignedInt64:
                case MessagePackCode.UnsignedInt64:
                case MessagePackCode.Real64:
                {
                    skipped += 1;
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    #region DrainValue

                    long bytesRead = 0;
                    while (8 > bytesRead)
                    {
                        var remaining = (8 - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    continue;
                }

                case MessagePackCode.Raw16:
                {
                    skipped += 1;
                    ushort length;
                    var    read = source.Read(buffer, 0, 2);
                    if (read == 2)
                    {
                        length   = BigEndianBinary.ToUInt16(buffer, 0);
                        skipped += 2;
                    }
                    else
                    {
                        return(null);
                    }
                    #region DrainValue

                    long bytesRead = 0;
                    while (length > bytesRead)
                    {
                        var remaining = (length - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    continue;
                }

                case MessagePackCode.Raw32:
                {
                    skipped += 1;
                    uint length;
                    var  read = source.Read(buffer, 0, 4);
                    if (read == 4)
                    {
                        length   = BigEndianBinary.ToUInt32(buffer, 0);
                        skipped += 4;
                    }
                    else
                    {
                        return(null);
                    }
                    #region DrainValue

                    long bytesRead = 0;
                    while (length > bytesRead)
                    {
                        var remaining = (length - bytesRead);
                        var reading   = remaining > DummyBufferForSkipping.Length ? DummyBufferForSkipping.Length : unchecked (( int )remaining);
                        bytesRead += source.Read(DummyBufferForSkipping, 0, reading);
                        if (bytesRead < reading)
                        {
                            return(null);
                        }
                    }

                    skipped += bytesRead;

                    #endregion DrainValue
                    #region TryPopContextCollection

                    remainingItems--;

                    if (remainingCollections != null)
                    {
                        while (remainingItems == 0 && remainingCollections.Count > 0)
                        {
                            if (remainingCollections.Count == 0)
                            {
                                break;
                            }

                            remainingItems = remainingCollections.Pop();
                            remainingItems--;
                        }
                    }

                    #endregion TryPopContextCollection
                    continue;
                }

                case MessagePackCode.Array16:
                {
                    skipped += 1;
                    ushort length;
                    var    read = source.Read(buffer, 0, 2);
                    if (read == 2)
                    {
                        length   = BigEndianBinary.ToUInt16(buffer, 0);
                        skipped += 2;
                    }
                    else
                    {
                        return(null);
                    }
                    if (length == 0)
                    {
                        #region TryPopContextCollection

                        remainingItems--;

                        if (remainingCollections != null)
                        {
                            while (remainingItems == 0 && remainingCollections.Count > 0)
                            {
                                if (remainingCollections.Count == 0)
                                {
                                    break;
                                }

                                remainingItems = remainingCollections.Pop();
                                remainingItems--;
                            }
                        }

                        #endregion TryPopContextCollection
                    }
                    else
                    {
                        #region PushContextCollection

                        if (remainingItems >= 0)
                        {
                            if (remainingCollections == null)
                            {
                                remainingCollections = new Int64Stack(4);
                            }

                            remainingCollections.Push(remainingItems);
                        }

                        remainingItems = length;

                        #endregion PushContextCollection
                    }

                    continue;
                }

                case MessagePackCode.Array32:
                {
                    skipped += 1;
                    uint length;
                    var  read = source.Read(buffer, 0, 4);
                    if (read == 4)
                    {
                        length   = BigEndianBinary.ToUInt32(buffer, 0);
                        skipped += 4;
                    }
                    else
                    {
                        return(null);
                    }
                    if (length == 0)
                    {
                        #region TryPopContextCollection

                        remainingItems--;

                        if (remainingCollections != null)
                        {
                            while (remainingItems == 0 && remainingCollections.Count > 0)
                            {
                                if (remainingCollections.Count == 0)
                                {
                                    break;
                                }

                                remainingItems = remainingCollections.Pop();
                                remainingItems--;
                            }
                        }

                        #endregion TryPopContextCollection
                    }
                    else
                    {
                        #region PushContextCollection

                        if (remainingItems >= 0)
                        {
                            if (remainingCollections == null)
                            {
                                remainingCollections = new Int64Stack(4);
                            }

                            remainingCollections.Push(remainingItems);
                        }

                        remainingItems = length;

                        #endregion PushContextCollection
                    }

                    continue;
                }

                case MessagePackCode.Map16:
                {
                    skipped += 1;
                    ushort length;
                    var    read = source.Read(buffer, 0, 2);
                    if (read == 2)
                    {
                        length   = BigEndianBinary.ToUInt16(buffer, 0);
                        skipped += 2;
                    }
                    else
                    {
                        return(null);
                    }
                    if (length == 0)
                    {
                        #region TryPopContextCollection

                        remainingItems--;

                        if (remainingCollections != null)
                        {
                            while (remainingItems == 0 && remainingCollections.Count > 0)
                            {
                                if (remainingCollections.Count == 0)
                                {
                                    break;
                                }

                                remainingItems = remainingCollections.Pop();
                                remainingItems--;
                            }
                        }

                        #endregion TryPopContextCollection
                    }
                    else
                    {
                        #region PushContextCollection

                        if (remainingItems >= 0)
                        {
                            if (remainingCollections == null)
                            {
                                remainingCollections = new Int64Stack(4);
                            }

                            remainingCollections.Push(remainingItems);
                        }

                        remainingItems = length * 2;

                        #endregion PushContextCollection
                    }

                    continue;
                }

                case MessagePackCode.Map32:
                {
                    skipped += 1;
                    uint length;
                    var  read = source.Read(buffer, 0, 4);
                    if (read == 4)
                    {
                        length   = BigEndianBinary.ToUInt32(buffer, 0);
                        skipped += 4;
                    }
                    else
                    {
                        return(null);
                    }
                    if (length == 0)
                    {
                        #region TryPopContextCollection

                        remainingItems--;

                        if (remainingCollections != null)
                        {
                            while (remainingItems == 0 && remainingCollections.Count > 0)
                            {
                                if (remainingCollections.Count == 0)
                                {
                                    break;
                                }

                                remainingItems = remainingCollections.Pop();
                                remainingItems--;
                            }
                        }

                        #endregion TryPopContextCollection
                    }
                    else
                    {
                        #region PushContextCollection

                        if (remainingItems >= 0)
                        {
                            if (remainingCollections == null)
                            {
                                remainingCollections = new Int64Stack(4);
                            }

                            remainingCollections.Push(remainingItems);
                        }

                        remainingItems = length * 2;

                        #endregion PushContextCollection
                    }

                    continue;
                }

                default:
                {
                    throw new MessageTypeException(String.Format(CultureInfo.CurrentCulture, "Unknown header value 0x{0:X}", header));
                }
                }
            } while (remainingItems > 0);

            return(skipped);
        }
        private async Task <MessagePackExtendedTypeObject> ReadMessagePackExtendedTypeObjectAsyncCore(ReadValueResult type, CancellationToken cancellationToken)
        {
            byte typeCode;
            uint length;

            switch (type)
            {
            case ReadValueResult.FixExt1:
            {
                typeCode = await this.ReadByteStrictAsync(cancellationToken).ConfigureAwait(false);

                length = 1;
                break;
            }

            case ReadValueResult.FixExt2:
            {
                typeCode = await this.ReadByteStrictAsync(cancellationToken).ConfigureAwait(false);

                length = 2;
                break;
            }

            case ReadValueResult.FixExt4:
            {
                typeCode = await this.ReadByteStrictAsync(cancellationToken).ConfigureAwait(false);

                length = 4;
                break;
            }

            case ReadValueResult.FixExt8:
            {
                typeCode = await this.ReadByteStrictAsync(cancellationToken).ConfigureAwait(false);

                length = 8;
                break;
            }

            case ReadValueResult.FixExt16:
            {
                typeCode = await this.ReadByteStrictAsync(cancellationToken).ConfigureAwait(false);

                length = 16;
                break;
            }

            case ReadValueResult.Ext8:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(byte), cancellationToken).ConfigureAwait(false);

                length   = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                typeCode = this.ReadByteStrict();
                break;
            }

            case ReadValueResult.Ext16:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(ushort), cancellationToken).ConfigureAwait(false);

                length   = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                typeCode = this.ReadByteStrict();
                break;
            }

            case ReadValueResult.Ext32:
            {
                await this.ReadStrictAsync(this._scalarBuffer, sizeof(uint), cancellationToken).ConfigureAwait(false);

                length   = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                typeCode = this.ReadByteStrict();
                break;
            }

            default:
            {
                this.ThrowUnexpectedExtCodeException(type);
                return(default(MessagePackExtendedTypeObject));                          // Never reach
            }
            }

            var data = new byte[length];

            await this.ReadStrictAsync(data, data.Length, cancellationToken).ConfigureAwait(false);

            this.InternalCollectionType = CollectionType.None;
            return(new MessagePackExtendedTypeObject(typeCode, data));
        }
        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);
            }
            }
        }
Exemplo n.º 7
0
        private MessagePackExtendedTypeObject ReadMessagePackExtendedTypeObjectCore(ReadValueResult type)
        {
            byte typeCode;
            uint length;

            switch (type)
            {
            case ReadValueResult.FixExt1:
            {
                typeCode = this.ReadByteStrict();
                length   = 1;
                break;
            }

            case ReadValueResult.FixExt2:
            {
                typeCode = this.ReadByteStrict();
                length   = 2;
                break;
            }

            case ReadValueResult.FixExt4:
            {
                typeCode = this.ReadByteStrict();
                length   = 4;
                break;
            }

            case ReadValueResult.FixExt8:
            {
                typeCode = this.ReadByteStrict();
                length   = 8;
                break;
            }

            case ReadValueResult.FixExt16:
            {
                typeCode = this.ReadByteStrict();
                length   = 16;
                break;
            }

            case ReadValueResult.Ext8:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(byte));
                length   = BigEndianBinary.ToByte(this._scalarBuffer, 0);
                typeCode = this.ReadByteStrict();
                break;
            }

            case ReadValueResult.Ext16:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(ushort));
                length   = BigEndianBinary.ToUInt16(this._scalarBuffer, 0);
                typeCode = this.ReadByteStrict();
                break;
            }

            case ReadValueResult.Ext32:
            {
                this.ReadStrict(this._scalarBuffer, sizeof(uint));
                length   = BigEndianBinary.ToUInt32(this._scalarBuffer, 0);
                typeCode = this.ReadByteStrict();
                break;
            }

            default:
            {
                ThrowUnexpectedExtCodeException(type);
                return(default(MessagePackExtendedTypeObject));                          // Never reach
            }
            }

            var data = new byte[length];

            this.ReadStrict(data, data.Length);
            this.InternalCollectionType = CollectionType.None;
            return(new MessagePackExtendedTypeObject(typeCode, data));
        }