Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.tomcat.util.http.fileupload;
 
 import static org.jboss.web.FileUploadMessages.MESSAGES;
 
 

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
       multipartStream.readBodyData(output);
       nextPart = multipartStream.readBoundary();
     }
   } catch(MultipartStream.MalformedStreamException e) {
     // the stream failed to follow required syntax
   } catch(IOException e) {
     // a read or write error occurred
   }
 

Author(s):
Rafal Krzewski
Martin Cooper
Sean C. Sullivan
Version:
$Id: MultipartStream.java 1533 2010-08-18 10:01:08Z remy.maucherat@jboss.com $
 
 public class MultipartStream {
    
Internal class, which is used to invoke the ProgressListener.
 
     public static class ProgressNotifier {
        
The listener to invoke.
 
         private final ProgressListener listener;
        
Number of expected bytes, if known, or -1.
        private final long contentLength;
        
Number of bytes, which have been read so far.
        private long bytesRead;
        
Number of items, which have been read so far.
        private int items;
        
Creates a new instance with the given listener and content length.

Parameters:
pListener The listener to invoke.
pContentLength The expected content length.
        ProgressNotifier(ProgressListener pListenerlong pContentLength) {
             = pListener;
             = pContentLength;
        }
        
Called to indicate that bytes have been read.

Parameters:
pBytes Number of bytes, which have been read.
        void noteBytesRead(int pBytes) {
            /* Indicates, that the given number of bytes have been read from
             * the input stream.
             */
             += pBytes;
            notifyListener();
        }
        
Called to indicate, that a new file item has been detected.
        void noteItem() {
            ++;
            notifyListener();
        }
        
Called for notifying the listener.
        private void notifyListener() {
            if ( != null) {
                .update();
            }
        }
    }
    // ----------------------------------------------------- Manifest constants


    
The Carriage Return ASCII character value.
    public static final byte CR = 0x0D;


    
The Line Feed ASCII character value.
    public static final byte LF = 0x0A;


    
The dash (-) ASCII character value.
    public static final byte DASH = 0x2D;


    
The maximum length of header-part that will be processed (10 kilobytes = 10240 bytes.).
    public static final int HEADER_PART_SIZE_MAX = 10240;


    
The default length of the buffer used for processing a request.
    protected static final int DEFAULT_BUFSIZE = 4096;


    
A byte sequence that marks the end of header-part (CRLFCRLF).
    protected static final byte[] HEADER_SEPARATOR = {
         };


    
A byte sequence that that follows a delimiter that will be followed by an encapsulation (CRLF).
    protected static final byte[] FIELD_SEPARATOR = {
        };


    
A byte sequence that that follows a delimiter of the last encapsulation in the stream (--).
    protected static final byte[] STREAM_TERMINATOR = {
        };


    
A byte sequence that precedes a boundary (CRLF--).
    protected static final byte[] BOUNDARY_PREFIX = {
        };
    // ----------------------------------------------------------- Data members


    
The input stream from which data is read.
    private final InputStream input;


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


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


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


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


    
The buffer used for processing the request.
    private final byte[] buffer;


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


    
The index of last valid character in the buffer + 1.
0 <= tail <= bufSize
    private int tail;


    
The content encoding to use when reading headers.
    private String headerEncoding;


    
The progress notifier, if any, or null.
    private final ProgressNotifier notifier;
    // ----------------------------------------------------------- Constructors

    

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.
pNotifier The notifier, which is used for calling the progress listener, if any.
See also:
MultipartStream(java.io.InputStream,byte[],org.apache.tomcat.util.http.fileupload.MultipartStream.ProgressNotifier)
            byte[] boundary,
            int bufSize,
            ProgressNotifier pNotifier) {
        this. = input;
        this. = bufSize;
        this. = new byte[bufSize];
        this. = pNotifier;
        // We prepend CR/LF to the boundary to chop trailing CR/LF from
        // body-data tokens.
        this. = boundary.length + .;
        if (bufSize < this. + 1) {
            throw .multipartStreamBufferSizeTooSmall();
        }
        this. = new byte[this.];
        this. = this..length;
        System.arraycopy(, 0, this., 0,
                .);
        System.arraycopy(boundary, 0, this..,
                boundary.length);
         = 0;
         = 0;
    }


    

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.
pNotifier An object for calling the progress listener, if any.
See also:
MultipartStream(java.io.InputStream,byte[],int,org.apache.tomcat.util.http.fileupload.MultipartStream.ProgressNotifier)
            byte[] boundary,
            ProgressNotifier pNotifier) {
        this(inputboundarypNotifier);
    }
    // --------------------------------------------------------- Public methods


    
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.
    public String getHeaderEncoding() {
        return ;
    }


    
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.
    public void setHeaderEncoding(String encoding) {
         = encoding;
    }


    
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.
    public byte readByte() throws IOException {
        // Buffer depleted ?
        if ( == ) {
             = 0;
            // Refill.
             = .read();
            if ( == -1) {
                // No more data available.
                throw new IOException(.noDataAvailable());
            }
            if ( != null) {
                .noteBytesRead();
            }
        }
        return [++];
    }


    
Skips a boundary token, and checks whether more encapsulations are contained in the stream.

Returns:
true if there are more encapsulations in this stream; false otherwise.
Throws:
MultipartStream.MalformedStreamException if the stream ends unexpecetedly or fails to follow required syntax.
    public boolean readBoundary()
            throws MalformedStreamException {
        byte[] marker = new byte[2];
        boolean nextChunk = false;
         += ;
        try {
            marker[0] = readByte();
            if (marker[0] == ) {
                // Work around IE5 Mac bug with input type=image.
                // Because the boundary delimiter, not including the trailing
                // CRLF, must not appear within any file (RFC 2046, section
                // 5.1.1), we know the missing CR is due to a buggy browser
                // rather than a file containing something similar to a
                // boundary.
                return true;
            }
            marker[1] = readByte();
            if (arrayequals(marker, 2)) {
                nextChunk = false;
            } else if (arrayequals(marker, 2)) {
                nextChunk = true;
            } else {
                throw new MalformedStreamException(.unexpectedCharactersAfterBoundary());
            }
        } catch (IOException e) {
            throw new MalformedStreamException(.unexpectedEndOfStream());
        }
        return nextChunk;
    }


    

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.
    public void setBoundary(byte[] boundary)
            throws IllegalBoundaryException {
        if (boundary.length !=  - .) {
            throw new IllegalBoundaryException(.invalidBoundaryToken());
        }
        System.arraycopy(boundary, 0, this..,
                boundary.length);
    }


    

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.
    public String readHeaders()
    throws MalformedStreamException {
        int i = 0;
        byte b;
        // to support multi-byte characters
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int size = 0;
        while (i < .) {
            try {
                b = readByte();
            } catch (IOException e) {
                throw new MalformedStreamException(.unexpectedEndOfStream());
            }
            if (++size > ) {
                throw new MalformedStreamException(.invalidHeader());
            }
            if (b == [i]) {
                i++;
            } else {
                i = 0;
            }
            baos.write(b);
        }
        String headers = null;
        if ( != null) {
            try {
                headers = baos.toString();
            } catch (UnsupportedEncodingException e) {
                // Fall back to platform default if specified encoding is not
                // supported.
                headers = baos.toString();
            }
        } else {
            headers = baos.toString();
        }
        return headers;
    }


    

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 MultipartStream(java.io.InputStream,byte[],int,org.apache.tomcat.util.http.fileupload.MultipartStream.ProgressNotifier)).

Parameters:
output The Stream to write data into. May be null, in which case this method is equivalent to discardBodyData().
Returns:
the amount of data written.
Throws:
MultipartStream.MalformedStreamException if the stream ends unexpectedly.
java.io.IOException if an i/o error occurs.
    public int readBodyData(OutputStream output)
            throws MalformedStreamExceptionIOException {
        final InputStream istream = newInputStream();
        return (int) Streams.copy(istreamoutputfalse);
    }

    
Creates a new MultipartStream.ItemInputStream.

Returns:
A new instance of MultipartStream.ItemInputStream.
        return new ItemInputStream();
    }

    

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.
    public int discardBodyData()
    IOException {
        return readBodyData(null);
    }


    
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.
    public boolean skipPreamble()
    throws IOException {
        // First delimiter may be not preceeded with a CRLF.
        System.arraycopy(, 2, , 0, . - 2);
         = . - 2;
        try {
            // Discard all data up to the delimiter.
            discardBodyData();
            // Read boundary - if succeeded, the stream contains an
            // encapsulation.
            return readBoundary();
        } catch (MalformedStreamException e) {
            return false;
        } finally {
            // Restore delimiter.
            System.arraycopy(, 0, , 2, . - 2);
             = .;
            [0] = ;
            [1] = ;
        }
    }


    
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.
    public static boolean arrayequals(byte[] a,
            byte[] b,
            int count) {
        for (int i = 0; i < counti++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }


    
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.
    protected int findByte(byte value,
            int pos) {
        for (int i = posi < i++) {
            if ([i] == value) {
                return i;
            }
        }
        return -1;
    }


    
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.
    protected int findSeparator() {
        int first;
        int match = 0;
        int maxpos =  - ;
        for (first = ;
        (first <= maxpos) && (match != );
        first++) {
            first = findByte([0], first);
            if (first == -1 || (first > maxpos)) {
                return -1;
            }
            for (match = 1; match < match++) {
                if ([first + match] != [match]) {
                    break;
                }
            }
        }
        if (match == ) {
            return first - 1;
        }
        return -1;
    }

    
Thrown to indicate that the input stream fails to follow the required syntax.
    public static class MalformedStreamException
            extends IOException {
        private static final long serialVersionUID = 1L;

        
Constructs a MalformedStreamException with no detail message.
        public MalformedStreamException() {
            super();
        }

        
Constructs an MalformedStreamException with the specified detail message.

Parameters:
message The detail message.
        public MalformedStreamException(String message) {
            super(message);
        }
    }


    
Thrown upon attempt of setting an invalid boundary token.
    public static class IllegalBoundaryException
            extends IOException {
        private static final long serialVersionUID = 1L;

        
Constructs an IllegalBoundaryException with no detail message.
        public IllegalBoundaryException() {
            super();
        }

        
Constructs an IllegalBoundaryException with the specified detail message.

Parameters:
message The detail message.
        public IllegalBoundaryException(String message) {
            super(message);
        }
    }

    
An java.io.InputStream for reading an items contents.
    public class ItemInputStream extends InputStream implements Closeable {
        
The number of bytes, which have been read so far.
        private long total;
        
The number of bytes, which must be hold, because they might be a part of the boundary.
        private int pad;
        
The current offset in the buffer.
        private int pos;
        
Whether the stream is already closed.
        private boolean closed;

        
Creates a new instance.
        ItemInputStream() {
            findSeparator();
        }

        
Called for finding the separator.
        private void findSeparator() {
             = MultipartStream.this.findSeparator();
            if ( == -1) {
                if ( -  > ) {
                     = ;
                } else {
                     =  - ;
                }
            }
        }

        
Returns the number of bytes, which have been read by the stream.

Returns:
Number of bytes, which have been read so far.
        public long getBytesRead() {
            return ;
        }

        
Returns the number of bytes, which are currently available, without blocking.

Returns:
Number of bytes in the buffer.
Throws:
java.io.IOException An I/O error occurs.
        @Override
        public int available() throws IOException {
            if ( == -1) {
                return  -  - ;
            }
            return  - ;
        }

        
Offset when converting negative bytes to integers.
        private static final int BYTE_POSITIVE_OFFSET = 256;

        
Returns the next byte in the stream.

Returns:
The next byte in the stream, as a non-negative integer, or -1 for EOF.
Throws:
java.io.IOException An I/O error occurred.
        @Override
        public int read() throws IOException {
            if () {
                throw new FileItemStream.ItemSkippedException();
            }
            if (available() == 0) {
                if (makeAvailable() == 0) {
                    return -1;
                }
            }
            ++;
            int b = [++];
            if (b >= 0) {
                return b;
            }
            return b + ;
        }

        
Reads bytes into the given buffer.

Parameters:
b The destination buffer, where to write to.
off Offset of the first byte in the buffer.
len Maximum number of bytes to read.
Returns:
Number of bytes, which have been actually read, or -1 for EOF.
Throws:
java.io.IOException An I/O error occurred.
        @Override
        public int read(byte[] bint offint lenthrows IOException {
            if () {
                throw new FileItemStream.ItemSkippedException();
            }
            if (len == 0) {
                return 0;
            }
            int res = available();
            if (res == 0) {
                res = makeAvailable();
                if (res == 0) {
                    return -1;
                }
            }
            res = Math.min(reslen);
            System.arraycopy(boffres);
             += res;
             += res;
            return res;
        }

        
Closes the input stream.

Throws:
java.io.IOException An I/O error occurred.
        @Override
        public void close() throws IOException {
            close(false);
        }

        
Closes the input stream.

Parameters:
pCloseUnderlying Whether to close the underlying stream (hard close)
Throws:
java.io.IOException An I/O error occurred.
        public void close(boolean pCloseUnderlyingthrows IOException {
            if () {
                return;
            }
            if (pCloseUnderlying) {
                 = true;
                .close();
            } else {
                for (;;) {
                    int av = available();
                    if (av == 0) {
                        av = makeAvailable();
                        if (av == 0) {
                            break;
                        }
                    }
                    skip(av);
                }
            }
             = true;
        }

        
Skips the given number of bytes.

Parameters:
bytes Number of bytes to skip.
Returns:
The number of bytes, which have actually been skipped.
Throws:
java.io.IOException An I/O error occurred.
        @Override
        public long skip(long bytesthrows IOException {
            if () {
                throw new FileItemStream.ItemSkippedException();
            }
            int av = available();
            if (av == 0) {
                av = makeAvailable();
                if (av == 0) {
                    return 0;
                }
            }
            long res = Math.min(avbytes);
             += res;
            return res;
        }

        
Attempts to read more data.

Returns:
Number of available bytes
Throws:
java.io.IOException An I/O error occurred.
        private int makeAvailable() throws IOException {
            if ( != -1) {
                return 0;
            }
            // Move the data to the beginning of the buffer.
             +=  -  - ;
            System.arraycopy( - , 0, );
            // Refill buffer with new data.
             = 0;
             = ;
            for (;;) {
                int bytesRead = .read( - );
                if (bytesRead == -1) {
                    // The last pad amount is left in the buffer.
                    // Boundary can't be in there so signal an error
                    // condition.
                    throw new MalformedStreamException(.unexpectedEndOfStream());
                }
                if ( != null) {
                    .noteBytesRead(bytesRead);
                }
                 += bytesRead;
                findSeparator();
                int av = available();
                if (av > 0 ||  != -1) {
                    return av;
                }
            }
        }

        
Returns, whether the stream is closed.

Returns:
True, if the stream is closed, otherwise false.
        public boolean isClosed() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X