# File lib/rdoc/encoding.rb, line 19
  def self.read_file filename, encoding, force_transcode = false
    content = open filename, "rb" do |f| f.read end

    utf8 = content.sub!(/\A\xef\xbb\xbf/, '')

    RDoc::Encoding.set_encoding content

    if Object.const_defined? :Encoding then
      encoding ||= Encoding.default_external
      orig_encoding = content.encoding

      if utf8 then
        content.force_encoding Encoding::UTF_8
        content.encode! encoding
      else
        # assume the content is in our output encoding
        content.force_encoding encoding
      end

      unless content.valid_encoding? then
        # revert and try to transcode
        content.force_encoding orig_encoding
        content.encode! encoding
      end

      unless content.valid_encoding? then
        warn "unable to convert #{filename} to #{encoding}, skipping"
        content = nil
      end
    end

    content
  rescue ArgumentError => e
    raise unless e.message =~ /unknown encoding name - (.*)/
    warn "unknown encoding name \"#{$1}\" for #{filename}, skipping"
    nil
  rescue Encoding::UndefinedConversionError => e
    if force_transcode then
      content.force_encoding orig_encoding
      content.encode! encoding, :undef => :replace, :replace => '?'
      content
    else
      warn "unable to convert #{e.message} for #{filename}, skipping"
      nil
    end
  rescue Errno::EISDIR, Errno::ENOENT
    nil
  end