示例#1
0
        public void Seek(long postion, BssomSeekOrgin orgin, long limit)
        {
            if (orgin == BssomSeekOrgin.Current)
            {
                Position += postion;
            }
            else
            {
                Position = postion;
            }

            if (limit != -1 && Position > limit)
            {
                BssomSerializationOperationException.ReaderEndOfBufferException();
            }

            if (Spans.Length == 1)
            {
                CurrentSpanPosition = (int)Position;
            }
            else
            {
                CurrentSpanIndex = Array.BinarySearch(SpansCumulativeBoundary, Position);
                if (CurrentSpanIndex <= -1)
                {
                    CurrentSpanIndex = ~CurrentSpanIndex - 1;
                }

                CurrentSpanPosition = (int)(Position - SpansCumulativeBoundary[CurrentSpanIndex]);
            }
        }
示例#2
0
 public void Seek(long postion, BssomSeekOrgin orgin = BssomSeekOrgin.Begin)
 {
     SeekWithOutVerify(postion, orgin);
     if (position > Length)
     {
         BssomSerializationOperationException.ReaderEndOfBufferException();
     }
 }
示例#3
0
 public void Seek(long postion, BssomSeekOrgin orgin)
 {
     SeekWithOutVerify(postion, orgin);
     if (_offset > _buffer.Length)
     {
         BssomSerializationOperationException.ReaderEndOfBufferException();
     }
 }
示例#4
0
        public ref byte ReadRef(int len, int position)
        {
            if (position + len > Boundary)
            {
                BssomSerializationOperationException.CapacityOfBufferIsInsufficient(len);
            }

            return(ref Buffer[Start + position]);
        }
        private static TKey GetKey(bool isNativeType, byte keyType, BssmapAnalysisStack stack, byte valueByteCount)
        {
            if (keyType != staticKeyResolver.KeyType && isNativeType != staticKeyResolver.KeyIsNativeType)
            {
                return(BssomSerializationOperationException.UnexpectedCodeRead <TKey>());
            }

            return(staticKeyResolver.ReadMap2Key(stack.ToUlongs((byte)valueByteCount)));
        }
        public static IBssMapKeyResolver GetAndVertiyBssMapKeyResolver(bool isNativeType, byte keyType)
        {
            IBssMapKeyResolver keyConvertor;

            if (isNativeType)
            {
                if (bssMapNativeTypeKeys.TryGetValue(keyType, out keyConvertor))
                {
                    return(keyConvertor);
                }
            }
            else
            {
                if (bssMapbuildInTypeKeys.TryGetValue(keyType, out keyConvertor))
                {
                    return(keyConvertor);
                }
            }

            return(BssomSerializationOperationException.UnexpectedCodeRead <IBssMapKeyResolver>());
        }
示例#7
0
        public static unsafe string GetSchemaString(ref BssomReader reader)
        {
            long positionWithOutMap2TypeHead(ref BssomReader r)
            {
                return(r.Position - 1);
            }

            MapSchemaStringBuilder msb        = new MapSchemaStringBuilder();
            BssMapHead             head       = BssMapHead.Read(ref reader);
            byte             nextKeyByteCount = 0;
            BssMapRouteToken t        = default;
            int count                 = head.ElementCount;
            BssmapAnalysisStack stack = new BssmapAnalysisStack(head.MaxDepth);
            AutomateState       state = AutomateState.ReadBranch;

            switch (state)
            {
            case AutomateState.ReadBranch:
            {
                t = reader.ReadMapToken();
                msb.AppendRouteToken(positionWithOutMap2TypeHead(ref reader) - 1, t);
                stack.PushToken(t);

                if (t >= BssMapRouteToken.EqualNext1 && t <= BssMapRouteToken.EqualNext8 ||
                    t >= BssMapRouteToken.EqualLast1 && t <= BssMapRouteToken.EqualLast8)
                {
                    if (t >= BssMapRouteToken.EqualNext1 && t <= BssMapRouteToken.EqualNext8)
                    {
                        long position = positionWithOutMap2TypeHead(ref reader);
                        reader.EnsureType(BssomBinaryPrimitives.FixUInt16);
                        msb.AppendNextOff(position, reader.ReadUInt16WithOutTypeHead());
                    }

                    nextKeyByteCount = BssMapRouteTokenHelper.GetEqualNextOrLastByteCount(t);
                    goto case AutomateState.ReadKey;
                }
                else if (t == BssMapRouteToken.EqualNextN || t == BssMapRouteToken.EqualLastN)
                {
                    if (t == BssMapRouteToken.EqualNextN)
                    {
                        long position = positionWithOutMap2TypeHead(ref reader);
                        reader.EnsureType(BssomBinaryPrimitives.FixUInt16);
                        msb.AppendNextOff(position, reader.ReadUInt16WithOutTypeHead());
                    }

                    ulong uint64Val = reader.ReadUInt64WithOutTypeHead();
                    msb.AppendUInt64Val(positionWithOutMap2TypeHead(ref reader) - 8, uint64Val);

                    goto case AutomateState.ReadBranch;
                }
                else if (t >= BssMapRouteToken.LessThen1 && t <= BssMapRouteToken.LessThen8)
                {
                    long position = positionWithOutMap2TypeHead(ref reader);
                    reader.EnsureType(BssomBinaryPrimitives.FixUInt16);
                    msb.AppendNextOff(position, reader.ReadUInt16WithOutTypeHead());

                    nextKeyByteCount = BssMapRouteTokenHelper.GetLessThenByteCount(t);

                    position = positionWithOutMap2TypeHead(ref reader);
                    if (nextKeyByteCount == 8)
                    {
                        msb.AppendUInt64Val(position, reader.ReadUInt64WithOutTypeHead());
                    }
                    else
                    {
                        msb.AppendUInt64Val(position, ref reader.BssomBuffer.ReadRef(nextKeyByteCount), nextKeyByteCount);
                        reader.BssomBuffer.SeekWithOutVerify(nextKeyByteCount, BssomSeekOrgin.Current);
                    }

                    goto case AutomateState.ReadBranch;
                }
                else if (t == BssMapRouteToken.LessElse)
                {
                    goto case AutomateState.ReadBranch;
                }
                else
                {
                    throw BssomSerializationOperationException.UnexpectedCodeRead((byte)t, reader.Position);
                }
            }

            case AutomateState.ReadKey:
            {
                if (nextKeyByteCount == 8)
                {
                    ulong uint64Val = reader.ReadUInt64WithOutTypeHead();
                    msb.AppendUInt64Val(positionWithOutMap2TypeHead(ref reader) - 8, uint64Val);
                }
                else
                {
                    //Read Raw(lessthan 8 byte)
                    ref byte ref1 = ref reader.BssomBuffer.ReadRef(nextKeyByteCount);
                    msb.AppendUInt64Val(positionWithOutMap2TypeHead(ref reader) - nextKeyByteCount, ref ref1, nextKeyByteCount);
                    reader.BssomBuffer.SeekWithOutVerify(nextKeyByteCount, BssomSeekOrgin.Current);
                }
                long position = positionWithOutMap2TypeHead(ref reader);
                byte keyType  = reader.ReadBssomType();
                if (keyType == BssomType.NativeCode)
                {
                    msb.AppendKeyType(position, true, reader.ReadBssomType());
                }
                else
                {
                    msb.AppendKeyType(position, false, keyType);
                }

                position = positionWithOutMap2TypeHead(ref reader);
                reader.EnsureType(BssomBinaryPrimitives.FixUInt32);
                msb.AppendValOffset(position, reader.ReadUInt32WithOutTypeHead());

                count--;
                goto case AutomateState.ReadChildren;
            }
示例#8
0
        public static object DeserializeBssObject(ref BssomReader reader, ref BssomDeserializeContext context,
                                                  bool isPriorityToDeserializeObjectAsBssomValue)
        {
            byte type = reader.SkipBlankCharacterAndPeekBssomType();

            switch (type)
            {
            case BssomType.NullCode:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(BssomNull.Value);
                }

                return(null);

            case BssomType.BooleanCode:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomBoolean(reader.ReadBooleanWithOutTypeHead()));
                }

                return(reader.ReadBooleanWithOutTypeHead());

            case BssomType.Int32Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadInt32WithOutTypeHead()));
                }

                return(reader.ReadInt32WithOutTypeHead());

            case BssomType.Int16Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadInt16WithOutTypeHead()));
                }

                return(reader.ReadInt16WithOutTypeHead());

            case BssomType.Int64Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadInt64WithOutTypeHead()));
                }

                return(reader.ReadInt64WithOutTypeHead());

            case BssomType.UInt64Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadUInt64WithOutTypeHead()));
                }

                return(reader.ReadUInt64WithOutTypeHead());

            case BssomType.UInt32Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadUInt32WithOutTypeHead()));
                }

                return(reader.ReadUInt32WithOutTypeHead());

            case BssomType.UInt16Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadUInt16WithOutTypeHead()));
                }

                return(reader.ReadUInt16WithOutTypeHead());

            case BssomType.UInt8Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadUInt8WithOutTypeHead()));
                }

                return(reader.ReadUInt8WithOutTypeHead());

            case BssomType.Int8Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomNumber(reader.ReadInt8WithOutTypeHead()));
                }

                return(reader.ReadInt8WithOutTypeHead());

            case BssomType.Float32Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomFloat(reader.ReadFloat32WithOutTypeHead()));
                }

                return(reader.ReadFloat32WithOutTypeHead());

            case BssomType.Float64Code:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomFloat(reader.ReadFloat64WithOutTypeHead()));
                }

                return(reader.ReadFloat64WithOutTypeHead());

            case BssomType.TimestampCode:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomDateTime(reader.ReadStandDateTimeWithOutTypeHead()));
                }

                return(reader.ReadStandDateTimeWithOutTypeHead());

            case BssomType.StringCode:
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                if (isPriorityToDeserializeObjectAsBssomValue)
                {
                    return(new BssomString(reader.ReadStringWithOutTypeHead()));
                }

                return(reader.ReadStringWithOutTypeHead());

            case BssomType.NativeCode:
            {
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                type = reader.ReadBssomType();
                switch (type)
                {
                case NativeBssomType.CharCode:
                    if (isPriorityToDeserializeObjectAsBssomValue)
                    {
                        return(new BssomChar(reader.ReadCharWithOutTypeHead()));
                    }

                    return(reader.ReadCharWithOutTypeHead());

                case NativeBssomType.DecimalCode:
                    if (isPriorityToDeserializeObjectAsBssomValue)
                    {
                        return(new BssomDecimal(reader.ReadDecimalWithOutTypeHead()));
                    }

                    return(reader.ReadDecimalWithOutTypeHead());

                case NativeBssomType.DateTimeCode:
                    if (isPriorityToDeserializeObjectAsBssomValue)
                    {
                        return(new BssomDateTime(reader.ReadNativeDateTimeWithOutTypeHead()));
                    }

                    return(reader.ReadNativeDateTimeWithOutTypeHead());

                case NativeBssomType.GuidCode:
                    if (isPriorityToDeserializeObjectAsBssomValue)
                    {
                        return(new BssomGuid(reader.ReadGuidWithOutTypeHead()));
                    }

                    return(reader.ReadGuidWithOutTypeHead());
                }
            }
            break;

            case BssomType.Array1:
            {
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                byte eleType = reader.ReadBssomType();
                if (eleType != BssomType.NativeCode)
                {
                    reader.BssomBuffer.Seek(-BssomBinaryPrimitives.Array1BuildInTypeCodeSize, BssomSeekOrgin.Current);
                }

                switch (eleType)
                {
                case BssomType.BooleanCode:
                    return(new BssomArray(BooleanListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.Int32Code:
                    return(new BssomArray(Int32ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.Int16Code:
                    return(new BssomArray(Int16ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.Int64Code:
                    return(new BssomArray(Int64ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.UInt64Code:
                    return(new BssomArray(UInt64ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.UInt32Code:
                    return(new BssomArray(UInt32ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.UInt16Code:
                    return(new BssomArray(UInt16ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.UInt8Code:
                    return(new BssomArray(UInt8ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.Int8Code:
                    return(new BssomArray(Int8ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.Float32Code:
                    return(new BssomArray(Float32ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.Float64Code:
                    return(new BssomArray(Float64ListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.TimestampCode:
                    return(new BssomArray(DateTimeListFormatter.Instance.Deserialize(ref reader, ref context), false));

                case BssomType.NativeCode:
                {
                    type = reader.ReadBssomType();
                    reader.BssomBuffer.Seek(-BssomBinaryPrimitives.Array1NativeTypeCodeSize, BssomSeekOrgin.Current);
                    switch (type)
                    {
                    case NativeBssomType.CharCode:
                        return(new BssomArray(CharListFormatter.Instance.Deserialize(ref reader, ref context), false));

                    case NativeBssomType.DecimalCode:
                        return(new BssomArray(DecimalListFormatter.Instance.Deserialize(ref reader, ref context), false));

                    case NativeBssomType.DateTimeCode:
                        return(new BssomArray(DateTimeListFormatter.Instance.Deserialize(ref reader, ref context), false));

                    case NativeBssomType.GuidCode:
                        return(new BssomArray(GuidListFormatter.Instance.Deserialize(ref reader, ref context), false));
                    }
                    break;
                }
                }
                break;
            }

            case BssomType.Array2:
            {
                reader.BssomBuffer.SeekWithOutVerify(1, BssomSeekOrgin.Current);
                reader.SkipVariableNumber();
                int           count = reader.ReadVariableNumber();
                List <object> ary   = new List <object>(count);
                for (int i = 0; i < count; i++)
                {
                    ary.Add(DeserializeBssObject(ref reader, ref context, isPriorityToDeserializeObjectAsBssomValue));
                }
                return(new BssomArray(ary, false));
            }

            case BssomType.Map1:
            case BssomType.Map2:
            {
                return(new BssomMap(MapFormatterHelper.GenericDictionaryDeserialize <object, object>(ref reader, ref context)));
            }
            }
            throw BssomSerializationOperationException.UnexpectedCodeRead(type, reader.Position);
        }