code: static String readGettextCharset(InputStream markableStream)
              throws IOException, UnsupportedEncodingException
      {
          String charset = "UTF-8";
          int limit = 4096;
          markableStream.mark(limit);
          byte[] buf = new byte[limit];
          int count = markableStream.read(buf);
          markableStream.reset();
          if (count < 0) {
              throw new RuntimeException();
          }
          String s = new String(buf, 0, count, "ASCII");
          Pattern pat =
                  Pattern.compile("\"Content-Type:.*charset=([^ \t\\\\]*)[ \t\\\\]");
          Matcher matcher = pat.matcher(s);
          if (matcher.find()) {
              charset = matcher.group(1);
          } else {
              // this regex is more prone to false positives inside comments, so
              // we try the more specific regex (above) first
              pat = Pattern.compile("charset=([^ \t\\\\]*)[ \t\\\\]");
              if (matcher.find()) {
                  charset = matcher.group(1);
              }
          }
          if (charset.equals("CHARSET")) {
              charset = "ASCII";
          }
          return charset;
      }
docs: Searches the beginning (4096 bytes) of the InputStream for a Gettext
  charset declaration, and returns the charset name. The InputStream must
  support "mark". It will be reset to the beginning of the stream. If no
  charset is found, UTF-8 will be assumed.
  <p>
  As with the Gettext tools, this only works for ASCII-compatible charsets.
  UTF-16 is not supported.
  </p>
  
  @param markableStream
  @return
  @throws IOException
  @throws UnsupportedEncodingException