Java Encode encodeGoomojiSubject(String subject)

Here you can find the source of encodeGoomojiSubject(String subject)

Description

encode Goomoji Subject

License

Open Source License

Declaration

public static String encodeGoomojiSubject(String subject) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*//from w  w  w  .j  a  v  a 2  s  .  co m
 * imoten - i mode.net mail tensou(forward)
 *
 * Copyright (C) 2010 shoozhoo (http://code.google.com/p/imoten/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

import java.io.UnsupportedEncodingException;

import javax.mail.internet.MimeUtility;

public class Main {
    public static String encodeGoomojiSubject(String subject) throws UnsupportedEncodingException {
        final int maxlen = 75 - ("=?UTF-8?B?".length() + "?=".length());
        StringBuilder sb = new StringBuilder();

        int mark = 0;
        int utf8len = "X-Goomoji-Subject: ".length();
        for (int i = 0; i < subject.length();) {
            int cp = subject.codePointAt(i);
            int len;
            if (cp < 0x7f)
                len = 1;
            else if (cp <= 0x7ff)
                len = 2;
            else if (cp <= 0xffff)
                len = 3;
            else
                len = 4;

            if (4 * ((utf8len + len - 1) / 3 + 1) >= maxlen) {
                if (mark > 0)
                    sb.append("\r\n ");
                sb.append(MimeUtility.encodeWord(subject.substring(mark, i), "UTF-8", "B"));
                mark = i;
                utf8len = 0;
            }

            utf8len += len;
            i += Character.charCount(cp);
        }
        if (mark > 0)
            sb.append("\r\n ");
        sb.append(MimeUtility.encodeWord(subject.substring(mark), "UTF-8", "B"));

        return sb.toString();
    }
}

Related

  1. encodedStringToProperties(String theEncodedPropertyString)
  2. encodeEmail(String s)
  3. encodeFile(String filePath)
  4. encodeForFilter(final String filterString)
  5. encodeForString(String str)
  6. encodeHeaderValue(String headerName, String headerValue)
  7. encodeHTML(String input, boolean reduceToASCII, boolean useHTMLTags)
  8. encodeHttpHeaderValueAsUTF8(String text)
  9. encodeImage(String sourceImagePath)