Class MultipartStream


  • public class MultipartStream
    extends java.lang.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 := "--" boudary "--"
    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) {
                header = chunks.readHeader();
                // process headers
                // create some output stream
                multipartStream.readBodyPart(output);
                nextPart = multipartStream.readBoundary();
            }
        } catch(MultipartStream.MalformedStreamException e) {
              // the stream failed to follow required syntax
        } catch(IOException) {
              // a read or write error occurred
        }
    
     
    Version:
    $Id: MultipartStream.java 353822 2005-12-04 06:33:55Z martinc $
    Author:
    Rafal Krzewski, Martin Cooper, Sean C. Sullivan
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] boundary
      The byte sequence that partitions the stream.
      protected static byte[] BOUNDARY_PREFIX
      A byte sequence that precedes a boundary (CRLF--).
      private int boundaryLength
      The length of the boundary token plus the leading CRLF--.
      private byte[] buffer
      The buffer used for processing the request.
      private int bufSize
      The length of the buffer used for processing the request.
      static byte CR
      The Carriage Return ASCII character value.
      static byte DASH
      The dash (-) ASCII character value.
      protected static int DEFAULT_BUFSIZE
      The default length of the buffer used for processing a request.
      protected static byte[] FIELD_SEPARATOR
      A byte sequence that that follows a delimiter that will be followed by an encapsulation (CRLF).
      private int head
      The index of first valid character in the buffer.
      static int HEADER_PART_SIZE_MAX
      The maximum length of header-part that will be processed (10 kilobytes = 10240 bytes.).
      protected static byte[] HEADER_SEPARATOR
      A byte sequence that marks the end of header-part (CRLFCRLF).
      private java.lang.String headerEncoding
      The content encoding to use when reading headers.
      private java.io.InputStream input
      The input stream from which data is read.
      private static int KEEP_REGION_PAD
      The number of bytes, over and above the boundary size, to use for the keep region.
      private int keepRegion
      The amount of data, in bytes, that must be kept in the buffer in order to detect delimiters reliably.
      static byte LF
      The Line Feed ASCII character value.
      protected static byte[] STREAM_TERMINATOR
      A byte sequence that that follows a delimiter of the last encapsulation in the stream (--).
      private int tail
      The index of last valid characer in the buffer + 1.
    • Constructor Summary

      Constructors 
      Constructor Description
      MultipartStream()
      Default constructor.
      MultipartStream​(java.io.InputStream input, byte[] boundary)
      Constructs a MultipartStream with a default size buffer.
      MultipartStream​(java.io.InputStream input, byte[] boundary, int bufSize)
      Constructs a MultipartStream with a custom size buffer.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean arrayequals​(byte[] a, byte[] b, int count)
      Compares count first bytes in the arrays a and b.
      int discardBodyData()
      Reads body-data from the current encapsulation and discards it.
      protected int findByte​(byte value, int pos)
      Searches for a byte of specified value in the buffer, starting at the specified position.
      protected int findSeparator()
      Searches for the boundary in the buffer region delimited by head and tail.
      java.lang.String getHeaderEncoding()
      Retrieves the character encoding used when reading the headers of an individual part.
      int readBodyData​(java.io.OutputStream output)
      Reads body-data from the current encapsulation and writes its contents into the output Stream.
      boolean readBoundary()
      Skips a boundary token, and checks whether more encapsulations are contained in the stream.
      byte readByte()
      Reads a byte from the buffer, and refills it as necessary.
      java.lang.String readHeaders()
      Reads the header-part of the current encapsulation.
      void setBoundary​(byte[] boundary)
      Changes the boundary token used for partitioning the stream.
      void setHeaderEncoding​(java.lang.String encoding)
      Specifies the character encoding to be used when reading the headers of individual parts.
      boolean skipPreamble()
      Finds the beginning of the first encapsulation.
      java.lang.String toString()
      Returns a string representation of this object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • CR

        public static final byte CR
        The Carriage Return ASCII character value.
        See Also:
        Constant Field Values
      • DASH

        public static final byte DASH
        The dash (-) ASCII character value.
        See Also:
        Constant Field Values
      • HEADER_PART_SIZE_MAX

        public static final int HEADER_PART_SIZE_MAX
        The maximum length of header-part that will be processed (10 kilobytes = 10240 bytes.).
        See Also:
        Constant Field Values
      • DEFAULT_BUFSIZE

        protected static final int DEFAULT_BUFSIZE
        The default length of the buffer used for processing a request.
        See Also:
        Constant Field Values
      • HEADER_SEPARATOR

        protected static final byte[] HEADER_SEPARATOR
        A byte sequence that marks the end of header-part (CRLFCRLF).
      • FIELD_SEPARATOR

        protected static final byte[] FIELD_SEPARATOR
        A byte sequence that that follows a delimiter that will be followed by an encapsulation (CRLF).
      • STREAM_TERMINATOR

        protected static final byte[] STREAM_TERMINATOR
        A byte sequence that that follows a delimiter of the last encapsulation in the stream (--).
      • BOUNDARY_PREFIX

        protected static final byte[] BOUNDARY_PREFIX
        A byte sequence that precedes a boundary (CRLF--).
      • KEEP_REGION_PAD

        private static final int KEEP_REGION_PAD
        The number of bytes, over and above the boundary size, to use for the keep region.
        See Also:
        Constant Field Values
      • input

        private java.io.InputStream input
        The input stream from which data is read.
      • boundaryLength

        private int boundaryLength
        The length of the boundary token plus the leading CRLF--.
      • keepRegion

        private int keepRegion
        The amount of data, in bytes, that must be kept in the buffer in order to detect delimiters reliably.
      • boundary

        private byte[] boundary
        The byte sequence that partitions the stream.
      • bufSize

        private int bufSize
        The length of the buffer used for processing the request.
      • buffer

        private byte[] buffer
        The buffer used for processing the request.
      • head

        private int head
        The index of first valid character in the buffer.
        0 <= head < bufSize
      • tail

        private int tail
        The index of last valid characer in the buffer + 1.
        0 <= tail <= bufSize
      • headerEncoding

        private java.lang.String headerEncoding
        The content encoding to use when reading headers.
    • Constructor Detail

      • MultipartStream

        public MultipartStream​(java.io.InputStream input,
                               byte[] boundary,
                               int bufSize)

        Constructs a MultipartStream with a custom size buffer.

        Note that the buffer must be at least big enough to contain the boundary string, plus 4 characters for CR/LF and double dash, plus at least one byte of data. Too small a buffer size setting will degrade performance.

        Parameters:
        input - The InputStream to serve as a data source.
        boundary - The token used for dividing the stream into encapsulations.
        bufSize - The size of the buffer to be used, in bytes.
        See Also:
        MultipartStream(), MultipartStream(InputStream, byte[])
      • MultipartStream

        public MultipartStream​(java.io.InputStream input,
                               byte[] boundary)
                        throws java.io.IOException

        Constructs a MultipartStream with a default size buffer.

        Parameters:
        input - The InputStream to serve as a data source.
        boundary - The token used for dividing the stream into encapsulations.
        Throws:
        java.io.IOException - when an error occurs.
        See Also:
        MultipartStream(), MultipartStream(InputStream, byte[], int)
    • Method Detail

      • getHeaderEncoding

        public java.lang.String getHeaderEncoding()
        Retrieves the character encoding used when reading the headers of an individual part. When not specified, or null, the platform default encoding is used.
        Returns:
        The encoding used to read part headers.
      • setHeaderEncoding

        public void setHeaderEncoding​(java.lang.String encoding)
        Specifies the character encoding to be used when reading the headers of individual parts. When not specified, or null, the platform default encoding is used.
        Parameters:
        encoding - The encoding used to read part headers.
      • readByte

        public byte readByte()
                      throws java.io.IOException
        Reads a byte from the buffer, and refills it as necessary.
        Returns:
        The next byte from the input stream.
        Throws:
        java.io.IOException - if there is no more data available.
      • setBoundary

        public void setBoundary​(byte[] boundary)
                         throws MultipartStream.IllegalBoundaryException

        Changes the boundary token used for partitioning the stream.

        This method allows single pass processing of nested multipart streams.

        The boundary token of the nested stream is required to be of the same length as the boundary token in parent stream.

        Restoring the parent stream boundary token after processing of a nested stream is left to the application.

        Parameters:
        boundary - The boundary to be used for parsing of the nested stream.
        Throws:
        MultipartStream.IllegalBoundaryException - if the boundary has a different length than the one being currently parsed.
      • readHeaders

        public java.lang.String readHeaders()
                                     throws MultipartStream.MalformedStreamException

        Reads the header-part of the current encapsulation.

        Headers are returned verbatim to the input stream, including the trailing CRLF marker. Parsing is left to the application.

        TODO allow limiting maximum header size to protect against abuse.

        Returns:
        The header-part of the current encapsulation.
        Throws:
        MultipartStream.MalformedStreamException - if the stream ends unexpecetedly.
      • readBodyData

        public int readBodyData​(java.io.OutputStream output)
                         throws MultipartStream.MalformedStreamException,
                                java.io.IOException

        Reads body-data from the current encapsulation and writes its contents into the output Stream.

        Arbitrary large amounts of data can be processed by this method using a constant size buffer. (see constructor).

        Parameters:
        output - The Stream to write data into.
        Returns:
        the amount of data written.
        Throws:
        MultipartStream.MalformedStreamException - if the stream ends unexpectedly.
        java.io.IOException - if an i/o error occurs.
      • discardBodyData

        public int discardBodyData()
                            throws MultipartStream.MalformedStreamException,
                                   java.io.IOException

        Reads body-data from the current encapsulation and discards it.

        Use this method to skip encapsulations you don't need or don't understand.

        Returns:
        The amount of data discarded.
        Throws:
        MultipartStream.MalformedStreamException - if the stream ends unexpectedly.
        java.io.IOException - if an i/o error occurs.
      • skipPreamble

        public boolean skipPreamble()
                             throws java.io.IOException
        Finds the beginning of the first encapsulation.
        Returns:
        true if an encapsulation was found in the stream.
        Throws:
        java.io.IOException - if an i/o error occurs.
      • arrayequals

        public static boolean arrayequals​(byte[] a,
                                          byte[] b,
                                          int count)
        Compares count first bytes in the arrays a and b.
        Parameters:
        a - The first array to compare.
        b - The second array to compare.
        count - How many bytes should be compared.
        Returns:
        true if count first bytes in arrays a and b are equal.
      • findByte

        protected int findByte​(byte value,
                               int pos)
        Searches for a byte of specified value in the buffer, starting at the specified position.
        Parameters:
        value - The value to find.
        pos - The starting position for searching.
        Returns:
        The position of byte found, counting from beginning of the buffer, or -1 if not found.
      • findSeparator

        protected int findSeparator()
        Searches for the boundary in the buffer region delimited by head and tail.
        Returns:
        The position of the boundary found, counting from the beginning of the buffer, or -1 if not found.
      • toString

        public java.lang.String toString()
        Returns a string representation of this object.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The string representation of this object.