Java Encode encodeQP(String text)

Here you can find the source of encodeQP(String text)

Description

encode QP

License

Open Source License

Declaration

static String encodeQP(String text) 

Method Source Code


//package com.java2s;
/*//  w w w  .j a va2  s. c  o m
 *  Copyright (c) 2011-2015 The original author or authors
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *       The Eclipse Public License is available at
 *       http://www.eclipse.org/legal/epl-v10.html
 *
 *       The Apache License v2.0 is available at
 *       http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */

import java.io.UnsupportedEncodingException;

import java.util.Locale;

public class Main {
    static String encodeQP(String text) {
        try {
            byte[] utf8 = text.getBytes("UTF-8");
            StringBuilder sb = new StringBuilder();

            int column = 0;
            for (int i = 0; i < utf8.length; i++) {
                char ch = (char) utf8[i];
                if (ch == '\n') {
                    sb.append(ch);
                    column = 0;
                } else {
                    boolean nextIsEOL = i == utf8.length - 1 || utf8[i + 1] == '\n';
                    String encChar;
                    if (mustEncode(ch) || nextIsEOL && ch == ' ') {
                        encChar = encodeChar(ch);
                    } else {
                        encChar = String.valueOf(ch);
                    }
                    int newColumn = column + encChar.length();
                    if (newColumn <= 75 || nextIsEOL && newColumn == 76) {
                        sb.append(encChar);
                        column = newColumn;
                    } else {
                        sb.append("=\n").append(encChar);
                        column = encChar.length();
                    }
                }
            }
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    static boolean mustEncode(char ch) {
        return ch >= 128 || ch < 10 || ch >= 11 && ch < 32 || ch == '=';
    }

    static boolean mustEncode(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != '=' && mustEncode(s.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    private static String encodeChar(char ch) {
        if (ch < 16) {
            return "=0" + Integer.toHexString(ch).toUpperCase(Locale.ENGLISH);
        } else {
            return '=' + Integer.toHexString(ch & 0xff).toUpperCase(Locale.ENGLISH);
        }
    }
}

Related

  1. encodeObject(Object obj)
  2. encodeOID(byte[] in, OutputStream os)
  3. encodeParameter(String name, String value, String encoding, String lang)
  4. encodePart(final String part, final String charset, final BitSet allowed)
  5. encodePersonal( InternetAddress[] addresses)
  6. encodeResult(final Object result)
  7. encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out)
  8. encodeStringBase64(String str)
  9. encodeTagChar(char c, Appendable buffer)