Mono.DataConverter.PackContext.Add C# (CSharp) Method

Add() public method

public Add ( byte group ) : void
group byte
return void
			public void Add (byte [] group)
			{
				//Console.WriteLine ("Adding {0} bytes to {1} (next={2}", group.Length,
				// buffer == null ? "null" : buffer.Length.ToString (), next);
				
				if (buffer == null){
					buffer = group;
					next = group.Length;
					return;
				}
				if (align != 0){
					if (align == -1)
						next = Align (next, group.Length);
					else
						next = Align (next, align);
					align = 0;
				}

				if (next + group.Length > buffer.Length){
					byte [] nb = new byte [System.Math.Max (next, 16) * 2 + group.Length];
					Array.Copy (buffer, nb, buffer.Length);
					Array.Copy (group, 0, nb, next, group.Length);
					next = next + group.Length;
					buffer = nb;
				} else {
					Array.Copy (group, 0, buffer, next, group.Length);
					next += group.Length;
				}
			}

Usage Example

コード例 #1
0
        private static bool PackOne(DataConverter.PackContext b, object oarg)
        {
            char c = b.description[b.i];

            switch (c)
            {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                b.repeat = (int)((short)b.description[b.i] - 48);
                return(false);

            default:
                switch (c)
                {
                case '[':
                {
                    int num = -1;
                    int i;
                    for (i = b.i + 1; i < b.description.Length; i++)
                    {
                        if (b.description[i] == ']')
                        {
                            break;
                        }
                        int num2 = (int)((short)b.description[i] - 48);
                        if (num2 >= 0 && num2 <= 9)
                        {
                            if (num == -1)
                            {
                                num = num2;
                            }
                            else
                            {
                                num = num * 10 + num2;
                            }
                        }
                    }
                    if (num == -1)
                    {
                        throw new ArgumentException("invalid size specification");
                    }
                    b.i      = i;
                    b.repeat = num;
                    return(false);
                }

                default:
                {
                    switch (c)
                    {
                    case '!':
                        b.align = -1;
                        return(false);

                    default:
                        switch (c)
                        {
                        case 'I':
                            b.Add(b.conv.GetBytes(Convert.ToUInt32(oarg)));
                            return(true);

                        default:
                            switch (c)
                            {
                            case 'x':
                                b.Add(new byte[1]);
                                return(false);

                            default:
                                if (c == '*')
                                {
                                    b.repeat = int.MaxValue;
                                    return(false);
                                }
                                if (c == 'S')
                                {
                                    b.Add(b.conv.GetBytes(Convert.ToUInt16(oarg)));
                                    return(true);
                                }
                                if (c != 's')
                                {
                                    throw new ArgumentException(string.Format("invalid format specified `{0}'", b.description[b.i]));
                                }
                                b.Add(b.conv.GetBytes(Convert.ToInt16(oarg)));
                                return(true);

                            case 'z':
                                break;
                            }
                            break;

                        case 'L':
                            b.Add(b.conv.GetBytes(Convert.ToUInt64(oarg)));
                            return(true);
                        }
                        break;

                    case '$':
                        break;

                    case '%':
                        b.conv = DataConverter.Native;
                        return(false);
                    }
                    bool flag = b.description[b.i] == 'z';
                    b.i++;
                    if (b.i >= b.description.Length)
                    {
                        throw new ArgumentException("$ description needs a type specified", "description");
                    }
                    char     c2 = b.description[b.i];
                    char     c3 = c2;
                    int      num2;
                    Encoding encoding;
                    switch (c3)
                    {
                    case '3':
                        encoding = Encoding.GetEncoding(12000);
                        num2     = 4;
                        break;

                    case '4':
                        encoding = Encoding.GetEncoding(12001);
                        num2     = 4;
                        break;

                    default:
                        if (c3 != 'b')
                        {
                            throw new ArgumentException("Invalid format for $ specifier", "description");
                        }
                        encoding = Encoding.BigEndianUnicode;
                        num2     = 2;
                        break;

                    case '6':
                        encoding = Encoding.Unicode;
                        num2     = 2;
                        break;

                    case '7':
                        encoding = Encoding.UTF7;
                        num2     = 1;
                        break;

                    case '8':
                        encoding = Encoding.UTF8;
                        num2     = 1;
                        break;
                    }
                    if (b.align == -1)
                    {
                        b.align = 4;
                    }
                    b.Add(encoding.GetBytes(Convert.ToString(oarg)));
                    if (flag)
                    {
                        b.Add(new byte[num2]);
                    }
                    break;
                }

                case '^':
                    b.conv = DataConverter.BigEndian;
                    return(false);

                case '_':
                    b.conv = DataConverter.LittleEndian;
                    return(false);

                case 'b':
                    b.Add(new byte[]
                    {
                        Convert.ToByte(oarg)
                    });
                    break;

                case 'c':
                    b.Add(new byte[]
                    {
                        (byte)Convert.ToSByte(oarg)
                    });
                    break;

                case 'd':
                    b.Add(b.conv.GetBytes(Convert.ToDouble(oarg)));
                    break;

                case 'f':
                    b.Add(b.conv.GetBytes(Convert.ToSingle(oarg)));
                    break;

                case 'i':
                    b.Add(b.conv.GetBytes(Convert.ToInt32(oarg)));
                    break;

                case 'l':
                    b.Add(b.conv.GetBytes(Convert.ToInt64(oarg)));
                    break;
                }
                break;

            case 'C':
                b.Add(new byte[]
                {
                    Convert.ToByte(oarg)
                });
                break;
            }
            return(true);
        }
DataConverter.PackContext