public static String decodeEncodedWords()

in core/src/main/java/org/apache/james/mime4j/codec/DecoderUtil.java [207:261]


    public static String decodeEncodedWords(String body, DecodeMonitor monitor, Charset fallback,
            Map<Charset, Charset> charsetOverrides)
            throws IllegalArgumentException {

        StringBuilder sb = new StringBuilder();
        int position = 0;

        while (position < body.length()) {
            int startPattern = body.indexOf("=?", position);
            if (startPattern < 0) {
                if (position == 0) {
                    return body;
                }
                sb.append(body, position, body.length());
                break;
            }

            int charsetEnd = body.indexOf('?', startPattern + 2);
            int encodingEnd = body.indexOf('?', charsetEnd + 1);
            int encodedTextEnd = body.indexOf("?=", encodingEnd + 1);

            if (charsetEnd < 0 || encodingEnd < 0 || encodedTextEnd < 0) {
                // Invalid pattern
                sb.append(body, position, startPattern + 2);
                position = startPattern + 2;
            } else if (encodingEnd == encodedTextEnd) {
                sb.append(body, position, Math.min(encodedTextEnd + 2, body.length()));
                position = encodedTextEnd +2;
            } else {
                String separator = body.substring(position, startPattern);
                if ((!CharsetUtil.isWhitespace(separator) || position == 0) && !separator.isEmpty()) {
                    sb.append(separator);
                }
                String mimeCharset = body.substring(startPattern + 2, charsetEnd);
                String encoding = body.substring(charsetEnd + 1, encodingEnd);
                String encodedText = body.substring(encodingEnd + 1, encodedTextEnd);

                if (encodedText.isEmpty()) {
                    position = encodedTextEnd + 2;
                    continue;
                }
                String decoded;
                decoded = tryDecodeEncodedWord(mimeCharset, encoding, encodedText, monitor, fallback, charsetOverrides);
                if (decoded != null) {
                    if (!CharsetUtil.isWhitespace(decoded) && !decoded.isEmpty()) {
                        sb.append(decoded);
                    }
                } else {
                    sb.append(body, startPattern, encodedTextEnd + 2);
                }
                position = encodedTextEnd + 2;
            }
        }
        return sb.toString();
    }