コード例 #1
0
ファイル: Encoding.cs プロジェクト: xhydongda/Easydata
        public static ClockValues Decode2(byte[] bytes, int startindex)
        {
            byte datatype = bytes[startindex];

            startindex++;
            int index;

            index = startindex + Varint.Read(bytes, startindex, out int tsLen);//时标段;
            TimeDecoder tdec = (TimeDecoder)DecoderFactory.Get(DataTypeEnum.DateTime);

            tdec.SetBytes(bytes, index, tsLen);
            index = index + tsLen;
            index = index + Varint.Read(bytes, index, out int vsLen);//数值段;
            IDecoder vdec = DecoderFactory.Get(datatype);

            vdec.SetBytes(bytes, index, vsLen);
            index = index + vsLen;
            index = index + Varint.Read(bytes, index, out int qsLen);//质量段;
            IntegerDecoder qdec = (IntegerDecoder)DecoderFactory.Get(DataTypeEnum.Integer);

            qdec.SetBytes(bytes, index, qsLen);
            ClockValues result = new ClockValues();

            if (datatype == DataTypeEnum.Double)
            {
                FloatDecoder decoder = (FloatDecoder)vdec;
                while (tdec.Next() && vdec.Next() && qdec.Next())
                {
                    result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read()));
                }
            }
            else if (datatype == DataTypeEnum.Boolean)
            {
                BooleanDecoder decoder = (BooleanDecoder)vdec;
                while (tdec.Next() && vdec.Next() && qdec.Next())
                {
                    result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read()));
                }
            }
            else if (datatype == DataTypeEnum.Integer)
            {
                IntegerDecoder decoder = (IntegerDecoder)vdec;
                while (tdec.Next() && vdec.Next() && qdec.Next())
                {
                    result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read()));
                }
            }
            else if (datatype == DataTypeEnum.String)
            {
                StringDecoder decoder = (StringDecoder)vdec;
                while (tdec.Next() && vdec.Next() && qdec.Next())
                {
                    result.Append(decoder.Create(tdec.Read(), decoder.Read(), (int)qdec.Read()));
                }
            }
            DecoderFactory.Put(DataTypeEnum.DateTime, tdec);
            DecoderFactory.Put(DataTypeEnum.Integer, qdec);
            DecoderFactory.Put(datatype, vdec);
            return(result);
        }
コード例 #2
0
        public string UnmarshalBinary(byte[] b, int startindex, int endindex)
        {
            Values.Clear();
            int i = startindex;

            while (i < endindex)
            {
                byte typ = b[i];
                i++;
                if (i + 8 > endindex)
                {
                    return(Constants.ErrWALCorrupt);
                }
                ulong sid = BitConverter.ToUInt64(b, i);
                i += 8;
                if (i + 4 > endindex)
                {
                    return(Constants.ErrWALCorrupt);
                }
                int nvals = BitConverter.ToInt32(b, i);
                i += 4;
                switch (typ)
                {
                case DataTypeEnum.Double:
                    if (i + 16 * nvals > endindex)
                    {
                        return(Constants.ErrWALCorrupt);
                    }
                    ClockValues values = new ClockValues(nvals);
                    for (int j = 0; j < nvals; j++)
                    {
                        long un = BitConverter.ToInt64(b, i);
                        i += 8;
                        double v = FloatDecoder.Float64frombits(BitConverter.ToUInt64(b, i));
                        i += 8;
                        int q = BitConverter.ToInt32(b, i);
                        i += 4;
                        values.Append(new ClockDouble(un, v, q));
                    }
                    Values[sid] = values;
                    break;

                case DataTypeEnum.Integer:
                    if (i + 16 * nvals > endindex)
                    {
                        return(Constants.ErrWALCorrupt);
                    }
                    values = new ClockValues(nvals);
                    for (int j = 0; j < nvals; j++)
                    {
                        long un = BitConverter.ToInt64(b, i);
                        i += 8;
                        long v = (long)BitConverter.ToUInt64(b, i);
                        i += 8;
                        int q = BitConverter.ToInt32(b, i);
                        i += 4;
                        values.Append(new ClockInt64(un, v, q));
                    }
                    Values[sid] = values;
                    break;

                case DataTypeEnum.Boolean:
                    if (i + 9 * nvals > endindex)
                    {
                        return(Constants.ErrWALCorrupt);
                    }
                    values = new ClockValues(nvals);
                    for (int j = 0; j < nvals; j++)
                    {
                        long un = BitConverter.ToInt64(b, i);
                        i += 8;
                        bool v = b[i] == 1 ? true : false;
                        i += 1;
                        int q = BitConverter.ToInt32(b, i);
                        i += 4;
                        values.Append(new ClockBoolean(un, v, q));
                    }
                    Values[sid] = values;
                    break;

                case DataTypeEnum.String:
                    values = new ClockValues(nvals);
                    for (int j = 0; j < nvals; j++)
                    {
                        if (i + 12 > endindex)
                        {
                            return(Constants.ErrWALCorrupt);
                        }
                        long un = BitConverter.ToInt64(b, i);
                        i += 8;
                        int length = BitConverter.ToInt32(b, i);
                        i += 4;
                        if (i + length > endindex)
                        {
                            return(Constants.ErrWALCorrupt);
                        }
                        string v = System.Text.Encoding.Default.GetString(b, i, length);
                        i += length;
                        int q = BitConverter.ToInt32(b, i);
                        i += 4;
                        values.Append(new ClockString(un, v, q));
                    }
                    Values[sid] = values;
                    break;

                default:
                    return(string.Format("unsupported value type: {0}", typ));
                }
            }
            return(null);
        }