Пример #1
0
        /// <summary>
        /// タグ情報を書き込みます。
        /// 既存のタグがなければ追加、ある場合は更新をおこないます。
        /// </summary>
        /// <param name="tag">タグ。</param>
        /// <param name="value">タグ情報。値を削除する場合は null を指定します。</param>
        /// <exception cref="ArgumentNullException">tag が null 参照です。</exception>
        /// <exception cref="NotSupportedException">指定されたタグは読み取り専用です。</exception>
        public void Write( AsfTagInfo tag, object value )
        {
            if( tag == null  ) { throw new ArgumentNullException( "tag" ); }
            if( !tag.CanEdit ) { throw new NotSupportedException( "Tag is read-only." ); }

            ObjectTagValue tagValue;
            if( value == null )
            {
                // 削除
                if( this._tags.TryGetValue( tag.Name, out tagValue ) )
                {
                    this.Size -= tagValue.Length;
                    this._tags.Remove( tag.Name );
                }
            }
            else
            {
                // 更新と追加
                if( this._tags.TryGetValue( tag.Name, out tagValue ) )
                {
                    // 更新
                    this.Size -= tagValue.Length;
                    tagValue = new ObjectTagValue( tag.Type, value );
                    this.Size += tagValue.Length;
                    this._tags[ tag.Name ] = tagValue;
                }
                else
                {
                    // 追加
                    tagValue = new ObjectTagValue( tag.Type, value );
                    this.Size += tagValue.Length;
                    this._tags.Add( tag.Name, tagValue );
                }
            }
        }
 /// <summary>
 /// タグ全体のサイズを算出します。
 /// </summary>
 /// <param name="name">タグの名前。</param>
 /// <param name="value">タグ情報。</param>
 /// <returns>サイズ。</returns>
 private static long CalcTagSize( string name, ObjectTagValue value )
 {
     // 名前の長さ ( 2 byte ) + 名前 + 値の型 ( 2 byte ) + 値の長さ ( 2 byte ) + 値
     var nameLength = Encoding.Unicode.GetByteCount( String.Concat( name, "\0" ) );
     return 2 + nameLength + 2 + 2 + value.Length;
 }