Here you can find the source of encodeQP(String text)
static String encodeQP(String text)
//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); } } }