예제 #1
0
        // We will have problem with binary blobs greater than int.MaxValue bytes.
        public byte[] Read(IMsgPackReader reader)
        {
            var type = reader.ReadDataType();

            uint length;

            switch (type)
            {
            case DataTypes.Null:
                return(null);

            case DataTypes.Bin8:
                length = NumberConverter.ReadUInt8(reader);
                break;

            case DataTypes.Bin16:
                length = NumberConverter.ReadUInt16(reader);
                break;

            case DataTypes.Bin32:
                length = NumberConverter.ReadUInt32(reader);
                break;

            case DataTypes.Str8:
                if (_compatibilityMode)
                {
                    length = NumberConverter.ReadUInt8(reader);
                }
                else
                {
                    throw ExceptionUtils.CantReadStringAsBinary();
                }
                break;

            case DataTypes.Str16:
                if (_compatibilityMode)
                {
                    length = NumberConverter.ReadUInt16(reader);
                }
                else
                {
                    throw ExceptionUtils.CantReadStringAsBinary();
                }
                break;

            case DataTypes.Str32:
                if (_compatibilityMode)
                {
                    length = NumberConverter.ReadUInt32(reader);
                }
                else
                {
                    throw ExceptionUtils.CantReadStringAsBinary();
                }
                break;

            default:
                if ((type & DataTypes.FixStr) == DataTypes.FixStr)
                {
                    if (_compatibilityMode)
                    {
                        length = (uint)(type & ~DataTypes.FixStr);
                    }
                    else
                    {
                        throw ExceptionUtils.CantReadStringAsBinary();
                    }
                }
                else
                {
                    throw ExceptionUtils.BadTypeException(type, DataTypes.Bin8, DataTypes.Bin16, DataTypes.Bin32, DataTypes.Null);
                }
                break;
            }

            var segment = reader.ReadBytes(length);
            var array   = new byte[segment.Count];

            Array.Copy(segment.Array, segment.Offset, array, 0, segment.Count);
            return(array);
        }