in core/src/main/java/org/apache/james/mime4j/codec/QuotedPrintableInputStream.java [177:294]
private int read0(final byte[] buffer, final int off, final int len) throws IOException {
boolean eof = false;
int to = off + len;
int index = off;
// check if a previous invocation left decoded content
if (decodedBuf.length() > 0) {
int chunk = Math.min(decodedBuf.length(), to - index);
System.arraycopy(decodedBuf.buffer(), 0, buffer, index, chunk);
decodedBuf.remove(0, chunk);
index += chunk;
}
while (index < to) {
if (limit - pos < 3) {
int bytesRead = fillBuffer();
eof = bytesRead == -1;
}
// end of stream?
if (limit - pos == 0 && eof) {
return index == off ? -1 : index - off;
}
while (pos < limit && index < to) {
int b = encoded[pos++] & 0xFF;
if (lastWasCR && b != LF) {
if (monitor.warn("Found CR without LF", "Leaving it as is")) {
throw new IOException("Found CR without LF");
}
index = transfer(CR, buffer, index, to, false);
} else if (!lastWasCR && b == LF) {
if (monitor.warn("Found LF without CR", "Translating to CRLF")) {
throw new IOException("Found LF without CR");
}
}
if (b == CR) {
lastWasCR = true;
continue;
} else {
lastWasCR = false;
}
if (b == LF) {
// at end of line
if (blanks.length() == 0) {
index = transfer(CR, buffer, index, to, false);
index = transfer(LF, buffer, index, to, false);
} else {
if (blanks.byteAt(0) != EQ) {
// hard line break
index = transfer(CR, buffer, index, to, false);
index = transfer(LF, buffer, index, to, false);
}
}
blanks.clear();
} else if (b == EQ) {
if (limit - pos < 2 && !eof) {
// not enough buffered data
pos--;
break;
}
// found special char '='
int b2 = getnext();
if (b2 == EQ) {
index = transfer(b2, buffer, index, to, true);
// deal with '==\r\n' brokenness
int bb1 = peek(0);
int bb2 = peek(1);
if (bb1 == LF || (bb1 == CR && bb2 == LF)) {
monitor.warn("Unexpected ==EOL encountered", "== 0x"+bb1+" 0x"+bb2);
blanks.append(b2);
} else {
monitor.warn("Unexpected == encountered", "==");
}
} else if (Character.isWhitespace((char) b2)) {
// soft line break
int b3 = peek(0);
if (!(b2 == CR && b3 == LF)) {
if (monitor.warn("Found non-standard soft line break", "Translating to soft line break")) {
throw new IOException("Non-standard soft line break");
}
}
if (b3 == LF) {
lastWasCR = b2 == CR;
}
index = transfer(-1, buffer, index, to, true);
if (b2 != LF) {
blanks.append(b);
blanks.append(b2);
}
} else {
int b3 = getnext();
int upper = convert(b2);
int lower = convert(b3);
if (upper < 0 || lower < 0) {
monitor.warn("Malformed encoded value encountered", "leaving "+((char) EQ)+((char) b2)+((char) b3)+" as is");
// TODO see MIME4J-160
index = transfer(EQ, buffer, index, to, true);
index = transfer(b2, buffer, index, to, false);
index = transfer(b3, buffer, index, to, false);
} else {
index = transfer((upper << 4) | lower, buffer, index, to, true);
}
}
} else if (Character.isWhitespace(b)) {
blanks.append(b);
} else {
index = transfer(b & 0xFF, buffer, index, to, true);
}
}
}
return to - off;
}