Hello Folks,

I happened to stumble upon a Bug in org.apache.james.mime4j.codec.DecoderUtil (mime4j version 0.6.0).

Proposed Test Case:

assertEquals("Test ü and more", DecoderUtil.decodeEncodedWords("Test =?ISO-8859-1?Q?=FC_?= =?ISO-8859-1?Q?and_more?="));

Proposed Quick and dirty Fix:

Change this line in DecoderUtil.decodeEncodedWords :
            int end = begin == -1 ? -1 : body.indexOf("?=", begin + 2);
to
int end = begin == -1 ? -1 : body.indexOf("?=", body.indexOf("?", begin + 2) + 3);

After this fix there is only one space between "ü" and "and", which I think is not correct (but I'm not sure).

Proposed Solution:

Replace "indexOf" by Regex matching, like so:

final static Pattern regex = Pattern.compile("(=\\?.*?\\?.*?\\?.*?\ \?=)");

    public static String decodeEncodedWords(String body) {
        StringBuffer sb = new StringBuffer();

        final Matcher matcher = regex.matcher(body);
        while (matcher.find()) {
matcher .appendReplacement (sb,decodeEncodedWord(body,matcher.start(),matcher.end()));
        }

        matcher.appendTail(sb);
        return sb.toString();
    }


Keep up the good work!
Aron


Reply via email to