private final static Pattern LITERAL_PATTERN = Pattern.compile(".*\\{(\\d+)\\}.*");
@Override
protected Request parseRequest(IMAPSession session, ByteBuffer buffer) throws Exception {
IMAPRequest request = new IMAPRequest(buffer);
Matcher matcher = LITERAL_PATTERN.matcher(request.getArgument());
if (matcher.matches()) {
final long bytesToRead = Long.parseLong(matcher.group(1));
MultiLineHandler<IMAPSession> handler = new MultiLineHandler<IMAPSession>() {
private static final String BYTES_READ = "BYTES_READ";
/*
* (non-Javadoc)
* @see org.apache.james.protocols.api.handler.MultiLineHandler#isReady(org.apache.james.protocols.api.ProtocolSession, java.nio.ByteBuffer)
*/
protected boolean isReady(IMAPSession session, ByteBuffer line) {
long bytesRead = (Long) session.setAttachment(BYTES_READ, null, State.Transaction);
bytesRead += line.remaining();
if (bytesRead >= bytesToRead) {
return true;
} else {
session.setAttachment(BYTES_READ, bytesRead, State.Transaction);
return false;
}
}
@Override
protected Response onLines(IMAPSession session, Collection<ByteBuffer> lines) {
session.popLineHandler();
return dispatchCommandHandlers(session, new IMAPRequest(lines));
}
};
buffer.rewind();
// push the line to the handler