public static String getCharsetNameFromContentType(String contentType) {
        // try optimized for direct match without using splitting
        int pos = contentType.indexOf("charset=");
        if (pos != -1) {
            // special optimization for utf-8 which is a common charset
            if (contentType.regionMatches(true, pos + 8, "utf-8", 0, 5)) {
                return "UTF-8";
            }

            int end = contentType.indexOf(';', pos);
            String charset;
            if (end > pos) {
                charset = contentType.substring(pos + 8, end);
            } else {
                charset = contentType.substring(pos + 8);
            }
            return normalizeCharset(charset);
        }

        String[] values = contentType.split(";");
        for (String value : values) {
            value = value.trim();
            // Perform a case insensitive "startsWith" check that works for different locales
            String prefix = "charset=";
            if (value.regionMatches(true, 0, prefix, 0, prefix.length())) {
                // Take the charset name
                String charset = value.substring(8);
                return normalizeCharset(charset);
            }
        }
        // use UTF-8 as default
        return "UTF-8";
    }
@Test
    public void testCharset() {
        assertEquals("UTF-8", IOHelper.getCharsetNameFromContentType("charset=utf-8"));
        assertEquals("UTF-8", IOHelper.getCharsetNameFromContentType("charset=UTF-8"));
        assertEquals("UTF-8", IOHelper.getCharsetNameFromContentType("text/plain; charset=UTF-8"));
        assertEquals("UTF-8", IOHelper.getCharsetNameFromContentType("application/json; charset=utf-8"));
        assertEquals("iso-8859-1", IOHelper.getCharsetNameFromContentType("application/json; charset=iso-8859-1"));
    }