diff --git a/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java b/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java index 724077c..4a8cc49 100644 --- a/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java +++ b/src/main/java/org/apache/james/mime4j/mboxiterator/FromLinePatterns.java @@ -28,11 +28,12 @@ public interface FromLinePatterns { /** * Match a line like: From ieugen@apache.org Fri Sep 09 14:04:52 2011 */ - static final String DEFAULT = "^From \\S+@\\S.*\\d{4}$"; - + // static final String DEFAULT = "^From \\S+@\\S.*\\d{4}$"; /** * Other type of From_ line: From MAILER-DAEMON Wed Oct 05 21:54:09 2011 + * Thunderbird mbox content: From - Wed Apr 02 06:51:08 2014 */ + static final String DEFAULT = "^From \\S+.*\\d{4}$"; } diff --git a/src/main/java/org/apache/james/mime4j/mboxiterator/MboxIterator.java b/src/main/java/org/apache/james/mime4j/mboxiterator/MboxIterator.java index 4b94c63..59c018a 100644 --- a/src/main/java/org/apache/james/mime4j/mboxiterator/MboxIterator.java +++ b/src/main/java/org/apache/james/mime4j/mboxiterator/MboxIterator.java @@ -46,7 +46,7 @@ public class MboxIterator implements Iterable, Closeable { private final FileInputStream theFile; private final CharBuffer mboxCharBuffer; - private Matcher fromLineMathcer; + private Matcher fromLineMatcher; private boolean fromLineFound; private final MappedByteBuffer byteBuffer; private final CharsetDecoder DECODER; @@ -58,6 +58,7 @@ public class MboxIterator implements Iterable, Closeable { private final Pattern MESSAGE_START; private int findStart = -1; private int findEnd = -1; + private final File mbox; private MboxIterator(final File mbox, final Charset charset, @@ -70,19 +71,28 @@ public class MboxIterator implements Iterable, Closeable { this.MESSAGE_START = Pattern.compile(regexpPattern, regexpFlags); this.DECODER = charset.newDecoder(); this.mboxCharBuffer = CharBuffer.allocate(MAX_MESSAGE_SIZE); + this.mbox=mbox; this.theFile = new FileInputStream(mbox); this.byteBuffer = theFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, theFile.getChannel().size()); initMboxIterator(); } - - private void initMboxIterator() throws IOException, CharConversionException { + + /** + * initialize the Mailbox iterator + * @throws IOException + * @throws CharConversionException + */ + protected void initMboxIterator() throws IOException, CharConversionException { decodeNextCharBuffer(); - fromLineMathcer = MESSAGE_START.matcher(mboxCharBuffer); - fromLineFound = fromLineMathcer.find(); + fromLineMatcher = MESSAGE_START.matcher(mboxCharBuffer); + fromLineFound = fromLineMatcher.find(); if (fromLineFound) { - saveFindPositions(fromLineMathcer); - } else if (fromLineMathcer.hitEnd()) { - throw new IllegalArgumentException("File does not contain From_ lines! Maybe not be a vaild Mbox."); + saveFindPositions(fromLineMatcher); + } else if (fromLineMatcher.hitEnd()) { + String path=""; + if (mbox!=null) + path=mbox.getPath(); + throw new IllegalArgumentException("File "+path+" does not contain From_ lines that match the pattern '"+MESSAGE_START.pattern()+"'! Maybe not be a valid Mbox."); } } @@ -139,12 +149,12 @@ public class MboxIterator implements Iterable, Closeable { */ public CharBufferWrapper next() { final CharBuffer message; - fromLineFound = fromLineMathcer.find(); + fromLineFound = fromLineMatcher.find(); if (fromLineFound) { message = mboxCharBuffer.slice(); message.position(findEnd + 1); - saveFindPositions(fromLineMathcer); - message.limit(fromLineMathcer.start()); + saveFindPositions(fromLineMatcher); + message.limit(fromLineMatcher.start()); } else { /* We didn't find other From_ lines this means either: * - we reached end of mbox and no more messages @@ -163,17 +173,17 @@ public class MboxIterator implements Iterable, Closeable { } catch (CharConversionException ex) { throw new RuntimeException(ex); } - fromLineMathcer = MESSAGE_START.matcher(mboxCharBuffer); - fromLineFound = fromLineMathcer.find(); + fromLineMatcher = MESSAGE_START.matcher(mboxCharBuffer); + fromLineFound = fromLineMatcher.find(); if (fromLineFound) { - saveFindPositions(fromLineMathcer); + saveFindPositions(fromLineMatcher); } message = mboxCharBuffer.slice(); - message.position(fromLineMathcer.end() + 1); - fromLineFound = fromLineMathcer.find(); + message.position(fromLineMatcher.end() + 1); + fromLineFound = fromLineMatcher.find(); if (fromLineFound) { - saveFindPositions(fromLineMathcer); - message.limit(fromLineMathcer.start()); + saveFindPositions(fromLineMatcher); + message.limit(fromLineMatcher.start()); } } else { message = mboxCharBuffer.slice();