in geronimo-mail_2.1_spec/src/main/java/org/apache/geronimo/mail/util/ASCIIUtil.java [85:147]
public static String getTextTransferEncoding(final InputStream content) throws IOException {
// for efficiency, we'll read in blocks.
final BufferedInputStream in = new BufferedInputStream(content, 4096);
int span = 0; // span of characters without a line break.
boolean containsLongLines = false;
int asciiChars = 0;
int nonAsciiChars = 0;
while (true) {
final int ch = in.read();
// if we hit an EOF here, go decide what type we've actually found.
if (ch == -1) {
break;
}
// we found a linebreak. Reset the line length counters on either one. We don't
// really need to validate here.
if (ch == '\n' || ch == '\r') {
// hit a line end, reset our line length counter
span = 0;
}
else {
span++;
// the text has long lines, we can't transfer this as unencoded text.
if (span > 998) {
containsLongLines = true;
}
// non-ascii character, we have to transfer this in binary.
if (!isAscii(ch)) {
nonAsciiChars++;
}
else {
asciiChars++;
}
}
}
// looking good so far, only valid chars here.
if (nonAsciiChars == 0) {
// does this contain long text lines? We need to use a Q-P encoding which will
// be only slightly longer, but handles folding the longer lines.
if (containsLongLines) {
return "quoted-printable";
}
else {
// ideal! Easiest one to handle.
return "7bit";
}
}
else {
// mostly characters requiring encoding? Base64 is our best bet.
if (nonAsciiChars > asciiChars) {
return "base64";
}
else {
// Q-P encoding will use fewer bytes than the full Base64.
return "quoted-printable";
}
}
}