예제 #1
0
        public void Encode(Bincoding value)
        {
            _s.WriteByte((byte)value.Type);

            switch (value.Type)
            {
            case BincodingType.NULL:
                break;

            case BincodingType.SHORTSTRING:
                _s.WriteByte(Convert.ToByte(value.Value.Length));
                _s.Write(value.Value, 0, value.Value.Length);
                break;

            case BincodingType.BINARY:
            case BincodingType.STRING:
                WriteLength(_s, value.Value.Length);
                _s.Write(value.Value, 0, value.Value.Length);
                break;

            case BincodingType.STREAM:
                Stream stream = value.GetValueStream();

                WriteLength(_s, Convert.ToInt32(stream.Length - stream.Position));
                stream.CopyTo(_s);
                break;

            case BincodingType.LIST:
                ICollection <Bincoding> list = value.GetList();

                WriteLength(_s, list.Count);

                foreach (Bincoding item in list)
                {
                    Encode(item);
                }

                break;

            case BincodingType.KEY_VALUE_PAIR:
                KeyValuePair <string, Bincoding> keyValue = value.GetKeyValuePair();

                byte[] keyBuffer = Encoding.UTF8.GetBytes(keyValue.Key);
                _s.WriteByte(Convert.ToByte(keyBuffer.Length));
                _s.Write(keyBuffer, 0, keyBuffer.Length);

                Encode(keyValue.Value);

                break;

            case BincodingType.DICTIONARY:
                IDictionary <string, Bincoding> dictionary = value.GetDictionary();

                WriteLength(_s, dictionary.Count);

                foreach (KeyValuePair <string, Bincoding> item in dictionary)
                {
                    EncodeKeyValue(item);
                }

                break;

            default:
                _s.Write(value.Value, 0, value.Value.Length);
                break;
            }
        }