--- ../james-protocols-old/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java 2012-10-13 13:38:00.000000000 +0100 +++ pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java 2013-02-10 23:52:23.000000000 +0000 @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.InputStream; -import java.io.SequenceInputStream; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -93,11 +92,9 @@ String uid = data.getUid(); if (deletedUidList.contains(uid) == false) { - InputStream body = new CountingBodyInputStream(new ExtraDotInputStream(new CRLFTerminatedInputStream(session.getUserMailbox().getMessageBody(uid))), lines); - InputStream headers = session.getUserMailbox().getMessageHeaders(uid); - if (body != null && headers != null) { - return new POP3StreamResponse(POP3Response.OK_RESPONSE, "Message follows", new SequenceInputStream(headers, body)); - + InputStream body = new CountingBodyInputStream(new ExtraDotInputStream(new CRLFTerminatedInputStream(session.getUserMailbox().getMessage(uid))), lines); + if (body != null) { + return new POP3StreamResponse(POP3Response.OK_RESPONSE, "Message follows", body); } else { StringBuilder exceptionBuffer = new StringBuilder(64).append("Message (").append(num).append(") does not exist."); return new POP3Response(POP3Response.ERR_RESPONSE, exceptionBuffer.toString()); @@ -143,8 +140,8 @@ } /** - * This {@link InputStream} implementation can be used to limit the body - * lines which will be read from the wrapped {@link InputStream} + * This {@link InputStream} implementation can be used to return all message headers + * and limit the body lines which will be read from the wrapped {@link InputStream}. */ private final class CountingBodyInputStream extends InputStream { @@ -152,6 +149,8 @@ private int limit = -1; private int lastChar; private InputStream in; + private boolean isBody = false; // starting from header + private boolean isEmptyLine = false; /** * @@ -171,9 +170,23 @@ if (count <= limit) { int a = in.read(); + // check for empty line + if (!isBody && isEmptyLine && lastChar == '\r' && a == '\n') { + // reached body + isBody = true; + } + if (lastChar == '\r' && a == '\n') { - count++; + // reset empty line flag + isEmptyLine = true; + + if (isBody) { + count++; + } + } else if (lastChar == '\n' && a != '\r') { + isEmptyLine = false; } + lastChar = a; return a;