Example usage for javax.mail.internet MimeUtility getDefaultJavaCharset

List of usage examples for javax.mail.internet MimeUtility getDefaultJavaCharset

Introduction

In this page you can find the example usage for javax.mail.internet MimeUtility getDefaultJavaCharset.

Prototype

public static String getDefaultJavaCharset() 

Source Link

Document

Get the default charset corresponding to the system's current default locale.

Usage

From source file:immf.Util.java

public static String encodeParameter(String name, String value, String encoding, String lang) {
    StringBuffer result = new StringBuffer();
    StringBuffer encodedPart = new StringBuffer();

    boolean needWriteCES = !isAllAscii(value);
    boolean CESWasWritten = false;
    boolean encoded;
    boolean needFolding = false;
    int sequenceNo = 0;
    int column;//from  www.ja v  a 2  s.co  m

    while (value.length() > 0) {
        // index of boundary of ascii/non ascii
        int lastIndex;
        boolean isAscii = value.charAt(0) < 0x80;
        for (lastIndex = 1; lastIndex < value.length(); lastIndex++) {
            if (value.charAt(lastIndex) < 0x80) {
                if (!isAscii)
                    break;
            } else {
                if (isAscii)
                    break;
            }
        }
        if (lastIndex != value.length())
            needFolding = true;

        RETRY: while (true) {
            encodedPart.delete(0, encodedPart.length());
            String target = value.substring(0, lastIndex);

            byte[] bytes;
            try {
                if (isAscii) {
                    bytes = target.getBytes("us-ascii");
                } else {
                    bytes = target.getBytes(encoding);
                }
            } catch (UnsupportedEncodingException e) {
                bytes = target.getBytes(); // use default encoding
                encoding = MimeUtility.mimeCharset(MimeUtility.getDefaultJavaCharset());
            }

            encoded = false;
            // It is not strict.
            column = name.length() + 7; // size of " " and "*nn*=" and ";"

            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] > ' ' && bytes[i] < 'z' && HeaderTokenizer.MIME.indexOf((char) bytes[i]) < 0) {
                    encodedPart.append((char) bytes[i]);
                    column++;
                } else {
                    encoded = true;
                    encodedPart.append('%');
                    String hex = Integer.toString(bytes[i] & 0xff, 16);
                    if (hex.length() == 1) {
                        encodedPart.append('0');
                    }
                    encodedPart.append(hex);
                    column += 3;
                }
                if (column > 76) {
                    needFolding = true;
                    lastIndex /= 2;
                    continue RETRY;
                }
            }

            result.append(";\r\n ").append(name);
            if (needFolding) {
                result.append('*').append(sequenceNo);
                sequenceNo++;
            }
            if (!CESWasWritten && needWriteCES) {
                result.append("*=");
                CESWasWritten = true;
                result.append(encoding).append('\'');
                if (lang != null)
                    result.append(lang);
                result.append('\'');
            } else if (encoded) {
                result.append("*=");
            } else {
                result.append('=');
            }
            result.append(new String(encodedPart));
            value = value.substring(lastIndex);
            break;
        }
    }
    return new String(result);
}