Skip to content

Instantly share code, notes, and snippets.

@korayguclu
Created May 3, 2013 12:20
Show Gist options
  • Save korayguclu/5508795 to your computer and use it in GitHub Desktop.
Save korayguclu/5508795 to your computer and use it in GitHub Desktop.
pars Character Encoding from contentType
// Taken from http://svn.apache.org/repos/asf/nutch/trunk/src/java/org/apache/nutch/util/EncodingDetector.java
/**
* Parse the character encoding from the specified content type header.
* If the content type is null, or there is no explicit character encoding,
* <code>null</code> is returned.
* <br />
* This method was copied from org.apache.catalina.util.RequestUtil,
* which is licensed under the Apache License, Version 2.0 (the "License").
*
* @param contentType a content type header
*/
public static String parseCharacterEncoding(String contentType) {
if (contentType == null)
return (null);
int start = contentType.indexOf("charset=");
if (start < 0)
return (null);
String encoding = contentType.substring(start + 8);
int end = encoding.indexOf(';');
if (end >= 0)
encoding = encoding.substring(0, end);
encoding = encoding.trim();
if ((encoding.length() > 2) && (encoding.startsWith("\""))
&& (encoding.endsWith("\"")))
encoding = encoding.substring(1, encoding.length() - 1);
return (encoding.trim());
}
@sosichlen
Copy link

ya ebal tvoy rot, suka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment