Quoted printable characters are encoded in the format =XX', where XX’ stands for the hexadecimal value of the character.
The encoder looks as follows:
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* It encodes data into quoted printable format.
*
* @author Daoyuan Li
*/
public class QuotedPrintableEncoder {
/**
* Encodes data into quoted printable format.
* @param s Data to be encoded.
* @return The encoded data.
*/
public String encode(String s) {
byte[] b = null;
try {
b = s.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
return "";
}
String code = "";
int wc = 0;
for (int i = 0; i < b.length; i++) {
byte c = b[i];
if(c == 13){
code = code.concat("\n");
wc = 0;
continue;
} else if(c == 10) {
//do nothing
continue;
}
code = code.concat("=" + Integer.toHexString(c &255).toUpperCase());
wc += 3;
if (wc >= 75) {
code = code.concat("=\n");
wc = 0;
}
}
return code;
}
/**
* Encodes data into quoted printable format, without soft line breaks.
* @param s Data to be encoded.
* @return The encoded data.
*/
public String encodeWithoutLineBreak(String s) {
byte[] b = null;
try{
b = s.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
return "";
}
String code = "";
for (int i = 0; i < b.length; i++) {
byte c = b[i];
if(c == 13){
code = code.concat("\n");
continue;
} else if (c == 10){
//do nothing
continue;
}
code = code.concat("=" + Integer.toHexString(c & 255).toUpperCase());
}
return code;
}
}
Leave a Comment