Beispiel #1
0
    public static int GetListIntBytesSize(List <int> lst, bool calcMember = false, int protoMember = 0)
    {
        int result;

        if (lst == null || lst.Count <= 0)
        {
            result = 0;
        }
        else
        {
            int ntag = 0;
            if (calcMember)
            {
                if (protoMember <= 15)
                {
                    ntag = 1;
                }
                else
                {
                    ntag = 2;
                }
            }
            int count = 0;
            count += ProtoUtil.CalcIntSize(lst.Count);
            int tc = lst.Count;
            for (int i = 0; i < tc; i++)
            {
                count += ProtoUtil.CalcIntSize(lst[i]);
            }
            result = ntag + count;
        }
        return(result);
    }
Beispiel #2
0
    public static int GetStringSize(string val, bool calcMember = false, int protoMember = 0)
    {
        int result;

        if (val == null)
        {
            result = 0;
        }
        else
        {
            int ntag = 0;
            if (calcMember)
            {
                if (protoMember <= 15)
                {
                    ntag = 1;
                }
                else
                {
                    ntag = 2;
                }
            }
            int len = val.Length;
            if (len == 0)
            {
                result = 1 + ntag;
            }
            else
            {
                int predicted = new UTF8Encoding().GetByteCount(val);
                result = ntag + ProtoUtil.CalcIntSize(predicted) + predicted;
            }
        }
        return(result);
    }
Beispiel #3
0
 public static byte[] ListToBytes <T>(List <T> lst, int member, ref int offset, byte[] data) where T : IProtoBuffData
 {
     byte[] result;
     if (lst == null || lst.Count <= 0)
     {
         result = data;
     }
     else
     {
         int tag = member << 3 | 2;
         offset += ProtoUtil.IntToBytes(data, offset, tag);
         offset += ProtoUtil.IntToBytes(data, offset, lst.Count);
         for (int i = 0; i < lst.Count; i++)
         {
             T      t     = lst[i];
             byte[] bytes = t.toBytes();
             int    ts    = ProtoUtil.CalcIntSize(bytes.Length);
             if (bytes.Length + offset + ts > data.Length)
             {
                 byte[] data2 = new byte[data.Length * 2];
                 Array.Copy(data, data2, data.Length);
                 data = data2;
             }
             offset += ProtoUtil.IntToBytes(data, offset, bytes.Length);
             if (bytes.Length > 0)
             {
                 Array.Copy(bytes, 0, data, offset, bytes.Length);
                 offset += bytes.Length;
             }
         }
         if (offset < data.Length)
         {
             Array.Resize <byte>(ref data, offset);
         }
         result = data;
     }
     return(result);
 }
Beispiel #4
0
    public static int GetListBytesSize <T>(List <T> lst, bool calcMember = false, int protoMember = 0) where T : IProtoBuffDataEx
    {
        int result;

        if (lst == null || lst.Count <= 0)
        {
            result = 0;
        }
        else
        {
            int ntag = 0;
            if (calcMember)
            {
                if (protoMember <= 15)
                {
                    ntag = 1;
                }
                else
                {
                    ntag = 2;
                }
            }
            int count = 0;
            count += ProtoUtil.CalcIntSize(lst.Count);
            int tc = lst.Count;
            for (int i = 0; i < tc; i++)
            {
                T   t  = lst[i];
                int ts = t.getBytesSize();
                count += ProtoUtil.CalcIntSize(ts);
                count += ts;
            }
            result = ntag + count;
        }
        return(result);
    }