📄 multipartstream.java
字号:
/* * 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.commons.fileupload;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import org.apache.commons.fileupload.util.Closeable;import org.apache.commons.fileupload.util.Streams;/** * <p> Low level API for processing file uploads. * * <p> This class can be used to process data streams conforming to MIME * 'multipart' format as defined in * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Arbitrarily * large amounts of data in the stream can be processed under constant * memory usage. * * <p> The format of the stream is defined in the following way:<br> * * <code> * multipart-body := preamble 1*encapsulation close-delimiter epilogue<br> * encapsulation := delimiter body CRLF<br> * delimiter := "--" boundary CRLF<br> * close-delimiter := "--" boudary "--"<br> * preamble := <ignore><br> * epilogue := <ignore><br> * body := header-part CRLF body-part<br> * header-part := 1*header CRLF<br> * header := header-name ":" header-value<br> * header-name := <printable ascii characters except ":"><br> * header-value := <any ascii characters except CR & LF><br> * body-data := <arbitrary data><br> * </code> * * <p>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 <strong>required</strong> to have a * boundary token of the same length as the parent stream (see {@link * #setBoundary(byte[])}). * * <p>Here is an example of usage of this class.<br> * * <pre> * 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 * } * * </pre> * * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> * @author <a href="mailto:martinc@apache.org">Martin Cooper</a> * @author Sean C. Sullivan * * @version $Id: MultipartStream.java 607869 2008-01-01 16:42:17Z jochen $ */public class MultipartStream { /** * Internal class, which is used to invoke the * {@link ProgressListener}. */ 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. * @param pListener The listener to invoke. * @param pContentLength The expected content length. */ ProgressNotifier(ProgressListener pListener, long pContentLength) { listener = pListener; contentLength = pContentLength; } /** Called to indicate that bytes have been read. * @param 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. */ bytesRead += pBytes; notifyListener(); } /** Called to indicate, that a new file item has been detected. */ void noteItem() { ++items; } /** Called for notifying the listener. */ private void notifyListener() { if (listener != null) { listener.update(bytesRead, contentLength, items); } } } // ----------------------------------------------------- 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 <code>header-part</code> 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 <code>header-part</code> * (<code>CRLFCRLF</code>). */ protected static final byte[] HEADER_SEPARATOR = { CR, LF, CR, LF }; /** * A byte sequence that that follows a delimiter that will be * followed by an encapsulation (<code>CRLF</code>). */ protected static final byte[] FIELD_SEPARATOR = { CR, LF}; /** * A byte sequence that that follows a delimiter of the last * encapsulation in the stream (<code>--</code>). */ protected static final byte[] STREAM_TERMINATOR = { DASH, DASH}; /** * A byte sequence that precedes a boundary (<code>CRLF--</code>). */ protected static final byte[] BOUNDARY_PREFIX = { CR, LF, DASH, DASH}; // ----------------------------------------------------------- Data members /** * The input stream from which data is read. */ private final InputStream input; /** * The length of the boundary token plus the leading <code>CRLF--</code>. */ 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. * <br> * 0 <= head < bufSize */ private int head; /** * The index of last valid characer in the buffer + 1. * <br> * 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 /** * Creates a new instance. * @deprecated Use {@link #MultipartStream(InputStream, byte[], * org.apache.commons.fileupload.MultipartStream.ProgressNotifier)}, * or {@link #MultipartStream(InputStream, byte[], int, * org.apache.commons.fileupload.MultipartStream.ProgressNotifier)} */ public MultipartStream() { this(null, null, null); } /** * <p> Constructs a <code>MultipartStream</code> with a custom size buffer * and no progress notifier. * * <p> 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. * * @param input The <code>InputStream</code> to serve as a data source. * @param boundary The token used for dividing the stream into * <code>encapsulations</code>. * @param bufSize The size of the buffer to be used, in bytes. * * @see #MultipartStream(InputStream, byte[], * MultipartStream.ProgressNotifier) * @deprecated Use {@link #MultipartStream(InputStream, byte[], int, * org.apache.commons.fileupload.MultipartStream.ProgressNotifier)}. */ public MultipartStream(InputStream input, byte[] boundary, int bufSize) { this(input, boundary, bufSize, null); } /** * <p> Constructs a <code>MultipartStream</code> with a custom size buffer. * * <p> 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. * * @param input The <code>InputStream</code> to serve as a data source. * @param boundary The token used for dividing the stream into * <code>encapsulations</code>. * @param bufSize The size of the buffer to be used, in bytes. * @param pNotifier The notifier, which is used for calling the * progress listener, if any. * * @see #MultipartStream(InputStream, byte[], * MultipartStream.ProgressNotifier) */ MultipartStream(InputStream input, byte[] boundary, int bufSize, ProgressNotifier pNotifier) { this.input = input; this.bufSize = bufSize; this.buffer = new byte[bufSize]; this.notifier = pNotifier; // We prepend CR/LF to the boundary to chop trailng CR/LF from // body-data tokens. this.boundary = new byte[boundary.length + BOUNDARY_PREFIX.length]; this.boundaryLength = boundary.length + BOUNDARY_PREFIX.length; this.keepRegion = this.boundary.length; System.arraycopy(BOUNDARY_PREFIX, 0, this.boundary, 0, BOUNDARY_PREFIX.length); System.arraycopy(boundary, 0, this.boundary, BOUNDARY_PREFIX.length, boundary.length); head = 0; tail = 0; } /** * <p> Constructs a <code>MultipartStream</code> with a default size buffer. * * @param input The <code>InputStream</code> to serve as a data source. * @param boundary The token used for dividing the stream into * <code>encapsulations</code>. * @param pNotifier An object for calling the progress listener, if any. * *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -