Java examples for Network:Http
Parse out a charset from a content type header.
//package com.java2s; import java.nio.charset.Charset; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static final Pattern charsetPattern = Pattern .compile("(?i)\\bcharset=\\s*\"?([^\\s;\"/]*)/?>"); /**//from www .j a va2 s. com * Parse out a charset from a content type header. If the charset is not supported, returns null (so the default * will kick in.) * * @param contentType e.g. "text/html; charset=EUC-JP" * @return "EUC-JP", or null if not found. Charset is trimmed and uppercased. */ public static String getCharsetFromContentType(String contentType) { if (contentType == null) return null; Matcher m = charsetPattern.matcher(contentType); if (m.find()) { String charset = m.group(1).trim(); if (Charset.isSupported(charset)) return charset; charset = charset.toUpperCase(Locale.ENGLISH); if (Charset.isSupported(charset)) return charset; } return null; } }