# File lib/mp3info.rb, line 372
  def close
    puts "close" if $DEBUG
    return unless @io_is_a_file
    if !@tag_parsing_enabled
      return 
    end
    if @tag != @tag_orig
      puts "@tag has changed" if $DEBUG

      # @tag1 has precedence over @tag
      if @tag1 == @tag1_orig
        @tag.each do |k, v|
          @tag1[k] = v
        end
      end

      # ruby-mp3info can only write v2.3 tags
      TAG_MAPPING_2_3.each do |key, tag2_name|
        @tag2.delete(TAG_MAPPING_2_2[key])
        @tag2[tag2_name] = @tag[key] if @tag[key]
      end
    end

    if @tag1 != @tag1_orig
      puts "@tag1 has changed" if $DEBUG
      raise(Mp3InfoError, "file is not writable") unless File.writable?(@filename_or_io)
      #@tag1_orig.update(@tag1)
      @tag1_orig = @tag1.dup
      File.open(@filename_or_io, 'rb+') do |file|
        if @tag1_orig.empty?
          newsize = @io_size - TAG1_SIZE
          file.truncate(newsize)
        else
          file.seek(-TAG1_SIZE, File::SEEK_END)
          t = file.read(3)
          if t != 'TAG'
            #append new tag
            file.seek(0, File::SEEK_END)
            file.write('TAG')
          end
          str = [
            @tag1_orig["title"]||"",
            @tag1_orig["artist"]||"",
            @tag1_orig["album"]||"",
            ((@tag1_orig["year"] != 0) ? ("%04d" % @tag1_orig["year"].to_i) : "\0\0\0\0"),
            @tag1_orig["comments"]||"",
            0,
            @tag1_orig["tracknum"]||0,
            @tag1_orig["genre"]||255
            ].pack("Z30Z30Z30Z4Z28CCC")
          file.write(str)
        end
      end
    end

    if @tag2.changed?
      puts "@tag2 has changed" if $DEBUG
      raise(Mp3InfoError, "file is not writable") unless File.writable?(@filename_or_io)
      tempfile_name = nil
      File.open(@filename_or_io, 'rb+') do |file|
        #if tag2 already exists, seek to end of it
        if @tag2.parsed?
          file.seek(@tag2.io_position)
        end
  #      if @io.read(3) == "ID3"
  #        version_maj, version_min, flags = @io.read(3).unpack("CCB4")
  #        unsync, ext_header, experimental, footer = (0..3).collect { |i| flags[i].chr == '1' }
  #        tag2_len = @io.get_syncsafe
  #        @io.seek(@io.get_syncsafe - 4, IO::SEEK_CUR) if ext_header
  #        @io.seek(tag2_len, IO::SEEK_CUR)
  #      end
        tempfile_name = @filename_or_io + ".tmp"
        File.open(tempfile_name, "wb") do |tempfile|
          unless @tag2.empty?
            tempfile.write(@tag2.to_bin)
          end

          bufsiz = file.stat.blksize || 4096
          while buf = file.read(bufsiz)
            tempfile.write(buf)
          end
        end
      end
      File.rename(tempfile_name, @filename_or_io)
    end
  end