public static String decodeEncodedWords()

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


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

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StringBuilder sb = new StringBuilder();
        int position = 0;
        String mimeCharset ="";
        String encoding ="";

        while (position < body.length()) {
            int startPattern = body.indexOf("=?", position);
            if (startPattern < 0) {
                if (position == 0) {
                    return body;
                }
                appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset);
                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
                appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset);
                sb.append(body, position, startPattern + 2);
                position = startPattern + 2;
            } else if (encodingEnd == encodedTextEnd) {
                appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset);
                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()) {
                    appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset);
                    sb.append(separator);
                }
                String mimeCurCharset = body.substring(startPattern + 2, charsetEnd);
                String curEncoding = body.substring(charsetEnd + 1, encodingEnd);
                String encodedText = body.substring(encodingEnd + 1, encodedTextEnd);

                if (!mimeCharset.isEmpty() && !mimeCurCharset.equals(mimeCharset)){
                    appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset);
                }

                if (!encoding.isEmpty() && !curEncoding.equals(encoding)){
                    appendStringBuffer(fallback, charsetOverrides, out, sb, mimeCharset);
                }

                mimeCharset=mimeCurCharset;
                encoding=curEncoding;

                if (encodedText.isEmpty()) {
                    position = encodedTextEnd + 2;
                    continue;
                }
                byte []decoded;

                decoded = tryDecodeEncodedWord(mimeCurCharset, curEncoding, encodedText, monitor, fallback, charsetOverrides);
                if (decoded != null) {
                    if (0 < decoded.length) {
                        try {
                            out.write(decoded);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                } else {
                    sb.append(body, startPattern, encodedTextEnd + 2);
                }
                position = encodedTextEnd + 2;
            }
        }
        appendStringBuffer(fallback, charsetOverrides, out, sb,mimeCharset);

        return sb.toString();
    }