• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handle big MIME multiparts with JavaMail

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the JavaMail MimeMultipart class (actually an extension thereof), for an application that is not mail, but uses MIME multipart HTTP POSTs to a Servlet. These POSTs can be really big (megabytes).
The supplied implementations of MimeBodyPart (used by MimeMultipart) seem to store the data in memory. I would like to extend MimeBodyPart to store the information on disk and only look at a small amount at a time.
I see already how I can (further) extend MimeMultipart to create a custom MimeBodyPart; there are overridable factory methods. But can anyone advise on best implementation of an extension of MimeBodyPart that can cope with really large amounts of data?
Has anyone already done this?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody replied, but I did figure out what I think is a good solution. I'll write just a little about it, in case it helps others.
JavaMail, as supplied in J2EE 1.4, includes special handling of data sources that provide data via a SharedInputStream. This is an interface that a custom InputStream subclass may implement, to say that it is able to create sub-streams for parts of the data. When the data is provided by a SharedInputStream, JavaMail does not read all the data into memory, so its memory footprint remains quite small, even if the request becomes very large.
I made use of this by implementing a class that would read the whole HTTP POST request and write it to disk file, then construct a SharedInputStream to serve up bits of the file, as requested by JavaMail.
It is slower than in-memory work, of course. Perhaps an ideal solution would try to determine whether the request was big enough to justify using a file; it is sometimes, but not always, possible to find this out, I think (an HTTP header may tell you).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!