class Nokogiri::HTML::Document::EncodingReader

def self.detect_encoding_for_jruby_without_fix(chunk)

def self.detect_encoding_for_jruby_without_fix(chunk)
  m = chunk.match(/\A(<\?xml[ \t\r\n]+[^>]*>)/) and
    return Nokogiri.XML(m[1]).encoding
  m = chunk.match(/(<meta\s)(.*)(charset\s*=\s*([\w-]+))(.*)/i) and
    return m[4]
  catch(:encoding_found) {
    Nokogiri::HTML::SAX::Parser.new(JumpSAXHandler.new(:encoding_found.to_s)).parse(chunk)
    nil
  }
rescue Nokogiri::SyntaxError, RuntimeError
  # Ignore parser errors that nokogiri may raise
  nil
end