in core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java [218:279]
private int fillBuffer() throws IOException {
if (eof) {
return -1;
}
int bytesRead;
if (!hasData()) {
bytesRead = buffer.fillBuffer();
if (bytesRead == -1) {
eof = true;
}
} else {
bytesRead = 0;
}
int i;
int off = buffer.pos();
for (;;) {
i = buffer.indexOf(boundary, off, buffer.limit() - off);
if (i == -1) {
break;
}
// Make sure the boundary is either at the very beginning of the buffer
// or preceded with LF
if (i == buffer.pos() || buffer.byteAt(i - 1) == '\n') {
int pos = i + boundary.length;
int remaining = buffer.limit() - pos;
if (remaining <= 0) {
// Make sure the boundary is terminated with EOS
break;
} else {
// or with a whitespace or '--'
char ch = (char)(buffer.byteAt(pos));
if (CharsetUtil.isWhitespace(ch)) {
break;
}
if (ch == '-' && remaining > 1 && (char)(buffer.byteAt(pos+1)) == '-') {
break;
}
}
}
off = i + boundary.length;
}
if (i != -1) {
limit = i;
atBoundary = true;
calculateBoundaryLen();
} else {
if (eof) {
limit = buffer.limit();
} else {
int position = buffer.limit() - (boundary.length + 3); // 2nd position before boundary
if (position >= 0 && (char) buffer.byteAt(position) == '\r') {
limit = buffer.limit() - (boundary.length + 3);
// boundary [CR][LF] minus two chars
} else {
limit = buffer.limit() - (boundary.length + 2);
// boundary [LF] minus one char
}
}
}
return bytesRead;
}