From e0b1d6c954c7c9654355e1f9f1ce1de617a914fa Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 7 Sep 2009 11:42:07 +0000 Subject: [PATCH] IMAP: fix bug 2835529 FETCH with unordered range git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@704 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/imap/ImapConnection.java | 70 ++++++++++++++++++----- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index af1eba62..5cd50887 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -1020,18 +1020,21 @@ public class ImapConnection extends AbstractConnection { } } - protected void skipToStartUid() { + protected void skipToNextRangeStartUid() { if (currentRangeIndex < ranges.length) { String currentRange = ranges[currentRangeIndex++]; int colonIndex = currentRange.indexOf(':'); if (colonIndex > 0) { startUid = convertToLong(currentRange.substring(0, colonIndex)); endUid = convertToLong(currentRange.substring(colonIndex + 1)); + if (endUid < startUid) { + long swap = endUid; + endUid = startUid; + startUid = swap; + } } else { startUid = endUid = convertToLong(currentRange); } - // Need to reset index for Palm pre - currentIndex = 0; while (currentIndex < currentFolder.count() && currentFolder.getImapUid(currentIndex) < startUid) { currentIndex++; } @@ -1040,13 +1043,31 @@ public class ImapConnection extends AbstractConnection { } } - public boolean hasNext() { - while (currentIndex < currentFolder.count() && currentFolder.getImapUid(currentIndex) > endUid) { - skipToStartUid(); - } + protected boolean hasNextInRange() { + return hasNextIndex() && currentFolder.getImapUid(currentIndex) <= endUid; + } + + protected boolean hasNextIndex() { return currentIndex < currentFolder.count(); } + protected boolean hasNextRange() { + return currentRangeIndex < ranges.length; + } + + public boolean hasNext() { + boolean hasNextInRange = hasNextInRange(); + // if has next range and current index after current range end, reset index + if (hasNextRange() && !hasNextInRange) { + currentIndex = 0; + } + while (hasNextIndex() && !hasNextInRange) { + skipToNextRangeStartUid(); + hasNextInRange = hasNextInRange(); + } + return hasNextIndex(); + } + public ExchangeSession.Message next() { ExchangeSession.Message message = currentFolder.get(currentIndex++); long uid = message.getImapUid(); @@ -1080,18 +1101,21 @@ public class ImapConnection extends AbstractConnection { } } - protected void skipToStartUid() { + protected void skipToNextRangeStart() { if (currentRangeIndex < ranges.length) { String currentRange = ranges[currentRangeIndex++]; int colonIndex = currentRange.indexOf(':'); if (colonIndex > 0) { startUid = convertToLong(currentRange.substring(0, colonIndex)); endUid = convertToLong(currentRange.substring(colonIndex + 1)); + if (endUid < startUid) { + long swap = endUid; + endUid = startUid; + startUid = swap; + } } else { startUid = endUid = convertToLong(currentRange); } - // Need to reset index for Palm pre - currentIndex = 0; while (currentIndex < currentFolder.count() && (currentIndex + 1) < startUid) { currentIndex++; } @@ -1100,13 +1124,31 @@ public class ImapConnection extends AbstractConnection { } } - public boolean hasNext() { - while (currentIndex < currentFolder.count() && (currentIndex + 1) > endUid) { - skipToStartUid(); - } + protected boolean hasNextInRange() { + return hasNextIndex() && currentIndex < endUid; + } + + protected boolean hasNextIndex() { return currentIndex < currentFolder.count(); } + protected boolean hasNextRange() { + return currentRangeIndex < ranges.length; + } + + public boolean hasNext() { + boolean hasNextInRange = hasNextInRange(); + // if has next range and current index after current range end, reset index + if (hasNextRange() && !hasNextInRange) { + currentIndex = 0; + } + while (hasNextIndex() && !hasNextInRange) { + skipToNextRangeStart(); + hasNextInRange = hasNextInRange(); + } + return hasNextIndex(); + } + public ExchangeSession.Message next() { return currentFolder.get(currentIndex++); }