classpathx-javamail
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Classpathx-javamail] Method MaildirFolder.appendMessages Optimizations


From: Conrad T. Pino
Subject: [Classpathx-javamail] Method MaildirFolder.appendMessages Optimizations
Date: Fri, 17 Jan 2014 16:40:05 -0800

  
I propose 2 optimizations to Subject method:

1.      Since Subject method is declared synchronized the synchronized block
consisting of lines 429-430 & 501 is pointless. 

2.      The if statements at lines 499 and 503 should be merged.

If the original intent was to conduct notifyMessageAddedListeners outside
the lock scope then the Subject method declaration should not be
synchronized.
 
This is a very small nit:

*       Line 500 reads: appended.toArray(n); 

*       I suggest "n = appended.toArray(n);" is a worthwhile defense against
non-compliant implementations.
*       I suppose implementations could be non-compliant in the other
direction too.
*       What does our collective experience suggest as the best practice?

Conrad Pino
  _____  

   419   /**
   420    * Appends messages to this folder.
   421    * Only MimeMessages within the array will be appended, as we don't
know
   422    * how to retrieve internet content for other kinds.
   423    * @param m an array of messages to be appended
   424    */
   425   public synchronized void appendMessages(Message[] m)
   426     throws MessagingException
   427   {
   428     MaildirMessage[] n;
   429     synchronized (this)
   430     {
   431       // make sure our message counts are up to date
   432       statDir(newdir);
   433       statDir(curdir);
   434       int nlen = newdir.messages.length;
   435       int clen = curdir.messages.length;
   436 
   437       List appended = new ArrayList(m.length);
   438       for (int i=0; i<m.length; i++)
   439       {
   440         if (m[i] instanceof MimeMessage)
   441         {
   442           MimeMessage src = (MimeMessage)m[i];
   443           Flags flags = src.getFlags();
   444           boolean seen = flags.contains(Flags.Flag.SEEN);
   445           int count = seen ? ++clen : ++nlen;
   446           try
   447           {
   448             String uniq = createUniq();
   449             String tmpname = uniq;
   450             String info = null;
   451             if (seen)
   452             {
   453               info = MaildirMessage.getInfo(flags);
   454               tmpname = new StringBuffer(uniq)
   455                 .append(':')
   456                 .append(info)
   457                 .toString();
   458             }
   459             File tmpfile = new File(tmpdir, tmpname);
   460             long time = System.currentTimeMillis();
   461             long timeout = time + 86400000L; // 24h
   462             while (time<timeout)
   463             {
   464               if (!tmpfile.exists())
   465                 break;
   466                try
   467                {
   468                  wait(2000); // sleep for 2s
   469                }
   470                catch (InterruptedException e)
   471                {
   472                }
   473               time = System.currentTimeMillis();
   474             }
   475             if (!tmpfile.createNewFile()) // create tmp/tmpname
   476               throw new MessagingException("Temporary file already
exists");
   477             OutputStream out =
   478               new BufferedOutputStream(new
FileOutputStream(tmpfile));
   479             src.writeTo(out); // write message to tmp/tmpname
   480             out.close(); // flush and close
   481             File file = new File(seen ? curdir.dir : newdir.dir,
tmpname);
   482             tmpfile.renameTo(file); // link to final location
   483             tmpfile.delete(); // delete temporary file
   484             MaildirMessage dst =
   485               new MaildirMessage(this, file, uniq, info, count);
   486             appended.add(dst);
   487           }
   488           catch (IOException e)
   489           {
   490             throw new MessagingException(e.getMessage(), e);
   491           }
   492           catch (SecurityException e)
   493           {
   494             throw new IllegalWriteException(e.getMessage());
   495           }
   496         }
   497       }
   498       n = new MaildirMessage[appended.size()];
   499       if (n.length>0)
   500         appended.toArray(n);
   501     }
   502     // propagate event
   503     if (n.length>0)
   504       notifyMessageAddedListeners(n);
   505   }

  _____  



reply via email to

[Prev in Thread] Current Thread [Next in Thread]