Class MultipartStream


public class MultipartStream extends Object

Low level API for processing file uploads.

This class can be used to process data streams conforming to MIME 'multipart' format as defined in RFC 1867. Arbitrarily large amounts of data in the stream can be processed under constant memory usage.

The format of the stream is defined in the following way:
multipart-body := preamble 1*encapsulation close-delimiter epilogue
encapsulation := delimiter body CRLF
delimiter := "--" boundary CRLF
close-delimiter := "--" boundary "--"
preamble := <ignore>
epilogue := <ignore>
body := header-part CRLF body-part
header-part := 1*header CRLF
header := header-name ":" header-value
header-name := <printable ascii characters except ":">
header-value := <any ascii characters except CR & LF>
body-data := <arbitrary data>

Note that body-data can contain another mulipart entity. There is limited support for single pass processing of such nested streams. The nested stream is required to have a boundary token of the same length as the parent stream (see setBoundary(byte[])).

Here is an example of usage of this class.

   try {
     MultipartStream multipartStream = new MultipartStream(input, boundary);
     boolean nextPart = multipartStream.skipPreamble();
     OutputStream output;
     while(nextPart) {
       String header = multipartStream.readHeaders();
       // process headers
       // create some output stream
       nextPart = multipartStream.readBoundary();
   } catch(MultipartStream.MalformedStreamException e) {
     // the stream failed to follow required syntax
   } catch(IOException e) {
     // a read or write error occurred