James Moger
2012-08-10 086c0447227a4075b66b976088542fee113b0d4f
Strip hidden UTF-8 BOM from string content
1 files modified
11 ■■■■ changed files
src/com/gitblit/utils/StringUtils.java 11 ●●●● patch | view | raw | blame | history
src/com/gitblit/utils/StringUtils.java
@@ -575,13 +575,15 @@
        if (!ArrayUtils.isEmpty(charsets)) {
            sets.addAll(Arrays.asList(charsets));
        }
        String value = null;
        sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
        for (String charset : sets) {
            try {
                Charset cs = Charset.forName(charset);
                CharsetDecoder decoder = cs.newDecoder();
                CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
                return buffer.toString();
                value = buffer.toString();
                break;
            } catch (CharacterCodingException e) {
                // ignore and advance to the next charset
            } catch (IllegalCharsetNameException e) {
@@ -590,6 +592,11 @@
                // ignore unsupported charsets
            }
        }
        return new String(content, Charset.forName("UTF-8"));
        value = new String(content, Charset.forName("UTF-8"));
        if (value.startsWith("\uFEFF")) {
            // strip UTF-8 BOM
            return value.substring(1);
        }
        return value;
    }
}