You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jo...@apache.org on 2006/11/27 02:06:54 UTC
svn commit: r479484 - in /jakarta/commons/proper/fileupload/trunk/src:
checkstyle/ java/org/apache/commons/fileupload/
java/org/apache/commons/fileupload/disk/
java/org/apache/commons/fileupload/portlet/
java/org/apache/commons/fileupload/servlet/ java...
Author: jochen
Date: Sun Nov 26 17:06:53 2006
New Revision: 479484
URL: http://svn.apache.org/viewvc?view=rev&rev=479484
Log:
Eliminated a lot of checkstyle warnings.
Added:
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/package.html
Modified:
jakarta/commons/proper/fileupload/trunk/src/checkstyle/fileupload_checks.xml
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/DiskFileUpload.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemStream.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUpload.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/MultipartStream.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/disk/DiskFileItem.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Closeable.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/LimitedInputStream.java
jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Streams.java
Modified: jakarta/commons/proper/fileupload/trunk/src/checkstyle/fileupload_checks.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/checkstyle/fileupload_checks.xml?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/checkstyle/fileupload_checks.xml (original)
+++ jakarta/commons/proper/fileupload/trunk/src/checkstyle/fileupload_checks.xml Sun Nov 26 17:06:53 2006
@@ -85,7 +85,10 @@
<!-- Checks for Javadoc comments. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
- <module name="JavadocMethod"/>
+ <module name="JavadocMethod">
+ <property name="allowUndeclaredRTE" value="true"/>
+ <property name="allowThrowsTagsForSubclasses" value="true"/>
+ </module>
<module name="JavadocType"/>
<module name="JavadocVariable"/>
<module name="JavadocStyle"/>
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/DiskFileUpload.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/DiskFileUpload.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/DiskFileUpload.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/DiskFileUpload.java Sun Nov 26 17:06:53 2006
@@ -80,6 +80,7 @@
* create <code>FileItem</code> instances.
*
* @see #DiskFileUpload()
+ * @param fileItemFactory The file item factory to use.
*
* @deprecated Use <code>FileUpload</code> instead.
*/
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemIterator.java Sun Nov 26 17:06:53 2006
@@ -15,26 +15,34 @@
* limitations under the License.
*/
package org.apache.commons.fileupload;
-
import java.io.IOException;
-import java.util.NoSuchElementException;
-/**
+/**
* An iterator, as returned by
- * {@link FileUploadBase#getItemIterator(RequestContext)}
+ * {@link FileUploadBase#getItemIterator(RequestContext)}.
*/
public interface FileItemIterator {
- /**
+ /**
* Returns, whether another instance of {@link FileItemStream}
* is available.
+ * @throws FileUploadException Parsing or processing the
+ * file item failed.
+ * @throws IOException Reading the file item failed.
+ * @return True, if one or more additional file items
+ * are available, otherwise false.
*/
boolean hasNext() throws FileUploadException, IOException;
- /**
+ /**
* Returns the next available {@link FileItemStream}.
- * @throws NoSuchElementException No more items are available. Use
+ * @throws java.util.NoSuchElementException No more items are available. Use
* {@link #hasNext()} to prevent this exception.
+ * @throws FileUploadException Parsing or processing the
+ * file item failed.
+ * @throws IOException Reading the file item failed.
+ * @return FileItemStream instance, which provides
+ * access to the next file item.
*/
FileItemStream next() throws FileUploadException, IOException;
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemStream.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemStream.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemStream.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileItemStream.java Sun Nov 26 17:06:53 2006
@@ -18,7 +18,6 @@
import java.io.IOException;
import java.io.InputStream;
-import java.util.Iterator;
/**
@@ -28,27 +27,34 @@
* <p>Instances of this class are created by accessing the
* iterator, returned by
* {@link FileUploadBase#getItemIterator(RequestContext)}.</p>
- * <em>Note</em>: There is an interaction between the iterator and
+ * <p><em>Note</em>: There is an interaction between the iterator and
* its associated instances of {@link FileItemStream}: By invoking
- * {@link Iterator#hasNext()} on the iterator, you discard all data,
- * which hasn't been read so far from the previous data.</p>
+ * {@link java.util.Iterator#hasNext()} on the iterator, you discard all data,
+ * which hasn't been read so far from the previous data.</p>
*/
public interface FileItemStream {
/**
* This exception is thrown, if an attempt is made to read
* data from the {@link InputStream}, which has been returned
* by {@link FileItemStream#openStream()}, after
- * {@link Iterator#hasNext()} has been invoked on the
+ * {@link java.util.Iterator#hasNext()} has been invoked on the
* iterator, which created the {@link FileItemStream}.
*/
public static class ItemSkippedException extends IOException {
+ /**
+ * The exceptions serial version UID, which is being used
+ * when serializing an exception instance.
+ */
private static final long serialVersionUID = -7280778431581963740L;
}
/** Creates an {@link InputStream}, which allows to read the
* items contents.
+ * @return The input stream, from which the items data may
+ * be read.
* @throws IllegalStateException The method was already invoked on
* this item. It is not possible to recreate the data stream.
+ * @throws IOException An I/O error occurred.
* @see ItemSkippedException
*/
InputStream openStream() throws IOException;
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUpload.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUpload.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUpload.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUpload.java Sun Nov 26 17:06:53 2006
@@ -16,7 +16,6 @@
*/
package org.apache.commons.fileupload;
-import javax.servlet.http.HttpServletRequest;
/**
* <p>High level API for processing file uploads.</p>
@@ -24,9 +23,9 @@
* <p>This class handles multiple files per single HTML widget, sent using
* <code>multipart/mixed</code> encoding type, as specified by
* <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
- * #parseRequest(HttpServletRequest)} to acquire a list of {@link
- * org.apache.commons.fileupload.FileItem}s associated with a given HTML
- * widget.</p>
+ * #parseRequest(javax.servlet.http.HttpServletRequest)} to acquire a list
+ * of {@link org.apache.commons.fileupload.FileItem FileItems} associated
+ * with a given HTML widget.</p>
*
* <p>How the data for individual parts is stored is determined by the factory
* used to create them; a given part may be in memory, on disk, or somewhere
@@ -73,6 +72,7 @@
* create <code>FileItem</code> instances.
*
* @see #FileUpload()
+ * @param fileItemFactory The factory to use for creating file items.
*/
public FileUpload(FileItemFactory fileItemFactory) {
super();
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadBase.java Sun Nov 26 17:06:53 2006
@@ -100,7 +100,7 @@
* @deprecated Use the method on <code>ServletFileUpload</code> instead.
*/
public static boolean isMultipartContent(HttpServletRequest req) {
- return ServletFileUpload.isMultipartContent(req);
+ return ServletFileUpload.isMultipartContent(req);
}
@@ -170,7 +170,7 @@
private String headerEncoding;
/**
- * The progress listener
+ * The progress listener.
*/
private ProgressListener listener;
@@ -227,9 +227,10 @@
* as opposed to {@link #getSizeMax()}.
*
* @see #setFileSizeMax(long)
+ * @return Maximum size of a single uploaded file.
*/
public long getFileSizeMax() {
- return fileSizeMax;
+ return fileSizeMax;
}
/**
@@ -237,9 +238,10 @@
* as opposed to {@link #getSizeMax()}.
*
* @see #getFileSizeMax()
+ * @param fileSizeMax Maximum size of a single uploaded file.
*/
public void setFileSizeMax(long fileSizeMax) {
- this.fileSizeMax = fileSizeMax;
+ this.fileSizeMax = fileSizeMax;
}
/**
@@ -286,7 +288,7 @@
* @deprecated Use the method in <code>ServletFileUpload</code> instead.
*/
public List /* FileItem */ parseRequest(HttpServletRequest req)
- throws FileUploadException {
+ throws FileUploadException {
return parseRequest(new ServletRequestContext(req));
}
@@ -307,7 +309,7 @@
* storing the uploaded content.
*/
public FileItemIterator getItemIterator(RequestContext ctx)
- throws FileUploadException, IOException {
+ throws FileUploadException, IOException {
return new FileItemIteratorImpl(ctx);
}
@@ -329,7 +331,6 @@
FileItemIterator iter = getItemIterator(ctx);
List items = new ArrayList();
FileItemFactory fac = getFileItemFactory();
- final byte[] buffer = new byte[8192];
while (iter.hasNext()) {
FileItemStream item = iter.next();
FileItem fileItem = fac.createItem(item.getFieldName(),
@@ -337,21 +338,21 @@
item.getName());
try {
Streams.copy(item.openStream(), fileItem.getOutputStream(),
- true, buffer);
+ true);
} catch (FileUploadIOException e) {
throw (FileUploadException) e.getCause();
} catch (IOException e) {
throw new IOFileUploadException(
- "Processing of " + MULTIPART_FORM_DATA
+ "Processing of " + MULTIPART_FORM_DATA
+ " request failed. " + e.getMessage(), e);
}
items.add(fileItem);
}
return items;
} catch (FileUploadIOException e) {
- throw (FileUploadException) e.getCause();
+ throw (FileUploadException) e.getCause();
} catch (IOException e) {
- throw new FileUploadException(e.getMessage(), e);
+ throw new FileUploadException(e.getMessage(), e);
}
}
@@ -461,55 +462,69 @@
* @return A <code>Map</code> containing the parsed HTTP request headers.
*/
protected Map /* String, String */ parseHeaders(String headerPart) {
- final int len = headerPart.length();
- Map headers = new HashMap();
+ final int len = headerPart.length();
+ Map headers = new HashMap();
int start = 0;
- for(;;) {
- int end = parseEndOfLine(headerPart, start);
- if (start == end) {
- break;
- }
- String header = headerPart.substring(start, end);
- start = end+2;
- while (start < len) {
- int nonWs = start;
- while (nonWs < len) {
- char c = headerPart.charAt(nonWs);
- if (c != ' ' && c != '\t') {
- break;
- }
- ++nonWs;
- }
- if (nonWs == start) {
- break;
- }
- // Continuation line found
- end = parseEndOfLine(headerPart, nonWs);
- header += " " + headerPart.substring(nonWs, end);
- start = end+2;
- }
- parseHeaderLine(headers, header);
+ for (;;) {
+ int end = parseEndOfLine(headerPart, start);
+ if (start == end) {
+ break;
+ }
+ String header = headerPart.substring(start, end);
+ start = end + 2;
+ while (start < len) {
+ int nonWs = start;
+ while (nonWs < len) {
+ char c = headerPart.charAt(nonWs);
+ if (c != ' ' && c != '\t') {
+ break;
+ }
+ ++nonWs;
+ }
+ if (nonWs == start) {
+ break;
+ }
+ // Continuation line found
+ end = parseEndOfLine(headerPart, nonWs);
+ header += " " + headerPart.substring(nonWs, end);
+ start = end + 2;
+ }
+ parseHeaderLine(headers, header);
}
return headers;
}
+ /**
+ * Skips bytes until the end of the current line.
+ * @param headerPart The headers, which are being parsed.
+ * @param end Index of the last byte, which has yet been
+ * processed.
+ * @return Index of the \r\n sequence, which indicates
+ * end of line.
+ */
private int parseEndOfLine(String headerPart, int end) {
- int index = end;
- for (;;) {
- int offset = headerPart.indexOf('\r', index);
- if (offset == -1 || offset+1 >= headerPart.length()) {
- throw new IllegalStateException("Expected headers to be terminated by an empty line.");
- }
- if (headerPart.charAt(offset+1) == '\n') {
- return offset;
- }
- index = offset+1;
- }
+ int index = end;
+ for (;;) {
+ int offset = headerPart.indexOf('\r', index);
+ if (offset == -1 || offset + 1 >= headerPart.length()) {
+ throw new IllegalStateException(
+ "Expected headers to be terminated by an empty line.");
+ }
+ if (headerPart.charAt(offset + 1) == '\n') {
+ return offset;
+ }
+ index = offset + 1;
+ }
}
-
+
+ /**
+ * Reads the next header line.
+ * @param headers String with all headers.
+ * @param header Map where to store the current header.
+ */
private void parseHeaderLine(Map headers, String header) {
- final int colonOffset = header.indexOf(':');
- if (colonOffset == -1) {
+ final int colonOffset = header.indexOf(':');
+ if (colonOffset == -1) {
// This header line is malformed, skip it.
return;
}
@@ -521,8 +536,8 @@
// More that one heder of that name exists,
// append to the list.
headers.put(headerName,
- getHeader(headers, headerName) + ','
- + headerValue);
+ getHeader(headers, headerName) + ','
+ + headerValue);
} else {
headers.put(headerName, headerValue);
}
@@ -539,7 +554,7 @@
* there were multiple headers of that name.
*/
protected final String getHeader(Map /* String, String */ headers,
- String name) {
+ String name) {
return (String) headers.get(name.toLowerCase());
}
@@ -548,12 +563,36 @@
* {@link FileUploadBase#getItemIterator(RequestContext)}.
*/
private class FileItemIteratorImpl implements FileItemIterator {
+ /**
+ * Default implementation of {@link FileItemStream}.
+ */
private class FileItemStreamImpl implements FileItemStream {
- private final String contentType, fieldName, name;
+ /** The file items content type.
+ */
+ private final String contentType;
+ /** The file items field name.
+ */
+ private final String fieldName;
+ /** The file items file name.
+ */
+ private final String name;
+ /** Whether the file item is a form field.
+ */
private final boolean formField;
+ /** The file items input stream.
+ */
private final InputStream stream;
+ /** Whether the file item was already opened.
+ */
private boolean opened;
+ /**
+ * CReates a new instance.
+ * @param pName The items file name, or null.
+ * @param pFieldName The items field name.
+ * @param pContentType The items content type, or null.
+ * @param pFormField Whether the item is a form field.
+ */
FileItemStreamImpl(String pName, String pFieldName,
String pContentType, boolean pFormField) {
name = pName;
@@ -562,39 +601,66 @@
formField = pFormField;
InputStream istream = multi.newInputStream();
if (fileSizeMax != -1) {
- istream = new LimitedInputStream(istream, fileSizeMax){
- protected void raiseError(long pSizeMax, long pCount) throws IOException {
- FileUploadException e = new FileSizeLimitExceededException(
- "The field " + fieldName + " exceeds its maximum permitted "
- + " size of " + pSizeMax + " characters.",
- pCount, pSizeMax);
- throw new FileUploadIOException(e);
-
- }
- };
+ istream = new LimitedInputStream(istream, fileSizeMax) {
+ protected void raiseError(long pSizeMax, long pCount)
+ throws IOException {
+ FileUploadException e =
+ new FileSizeLimitExceededException(
+ "The field " + fieldName
+ + " exceeds its maximum permitted "
+ + " size of " + pSizeMax
+ + " characters.",
+ pCount, pSizeMax);
+ throw new FileUploadIOException(e);
+ }
+ };
}
stream = istream;
}
+ /**
+ * Returns the items content type, or null.
+ * @return Content type, if known, or null.
+ */
public String getContentType() {
return contentType;
}
+ /**
+ * Returns the items field name.
+ * @return Field name.
+ */
public String getFieldName() {
return fieldName;
}
+ /**
+ * Returns the items file name.
+ * @return File name, if known, or null.
+ */
public String getName() {
return name;
}
+ /**
+ * Returns, whether this is a form field.
+ * @return True, if the item is a form field,
+ * otherwise false.
+ */
public boolean isFormField() {
return formField;
}
+ /**
+ * Returns an input stream, which may be used to
+ * read the items contents.
+ * @return Opened input stream.
+ * @throws IOException An I/O error occurred.
+ */
public InputStream openStream() throws IOException {
if (opened) {
- throw new IllegalStateException("The stream was already opened.");
+ throw new IllegalStateException(
+ "The stream was already opened.");
}
if (((Closeable) stream).isClosed()) {
throw new FileItemStream.ItemSkippedException();
@@ -602,74 +668,118 @@
return stream;
}
+ /**
+ * Closes the file item.
+ * @throws IOException An I/O error occurred.
+ */
void close() throws IOException {
stream.close();
}
}
+ /**
+ * The multi part stream to process.
+ */
private final MultipartStream multi;
+ /**
+ * The notifier, which used for triggering the
+ * {@link ProgressListener}.
+ */
private final MultipartStream.ProgressNotifier notifier;
+ /**
+ * The boundary, which separates the various parts.
+ */
private final byte[] boundary;
+ /**
+ * The item, which we currently process.
+ */
private FileItemStreamImpl currentItem;
+ /**
+ * The current items field name.
+ */
private String currentFieldName;
+ /**
+ * Whether we are currently skipping the preamble.
+ */
private boolean skipPreamble;
+ /**
+ * Whether the current item may still be read.
+ */
private boolean itemValid;
+ /**
+ * Whether we have seen the end of the file.
+ */
private boolean eof;
- FileItemIteratorImpl(RequestContext ctx) throws FileUploadException, IOException {
+ /**
+ * Creates a new instance.
+ * @param ctx The request context.
+ * @throws FileUploadException An error occurred while
+ * parsing the request.
+ * @throws IOException An I/O error occurred.
+ */
+ FileItemIteratorImpl(RequestContext ctx)
+ throws FileUploadException, IOException {
if (ctx == null) {
throw new NullPointerException("ctx parameter");
}
String contentType = ctx.getContentType();
if ((null == contentType)
- || (!contentType.toLowerCase().startsWith(MULTIPART))) {
+ || (!contentType.toLowerCase().startsWith(MULTIPART))) {
throw new InvalidContentTypeException(
- "the request doesn't contain a "
- + MULTIPART_FORM_DATA
- + " or "
- + MULTIPART_MIXED
- + " stream, content type header is "
- + contentType);
+ "the request doesn't contain a "
+ + MULTIPART_FORM_DATA
+ + " or "
+ + MULTIPART_MIXED
+ + " stream, content type header is "
+ + contentType);
}
InputStream input = ctx.getInputStream();
if (sizeMax >= 0) {
- int requestSize = ctx.getContentLength();
- if (requestSize == -1) {
- input = new LimitedInputStream(input, sizeMax){
- protected void raiseError(long pSizeMax, long pCount) throws IOException {
- FileUploadException ex = new SizeLimitExceededException(
- "the request was rejected because its size (" + pCount
- + ") exceeds the configured maximum (" + pSizeMax + ")",
- pCount, pSizeMax);
- throw new FileUploadIOException(ex);
- }
- };
- } else {
- if (sizeMax >= 0 && requestSize > sizeMax) {
- throw new SizeLimitExceededException(
- "the request was rejected because its size (" + requestSize
- + ") exceeds the configured maximum (" + sizeMax + ")",
- requestSize, sizeMax);
- }
- }
+ int requestSize = ctx.getContentLength();
+ if (requestSize == -1) {
+ input = new LimitedInputStream(input, sizeMax) {
+ protected void raiseError(long pSizeMax, long pCount)
+ throws IOException {
+ FileUploadException ex =
+ new SizeLimitExceededException(
+ "the request was rejected because"
+ + " its size (" + pCount
+ + ") exceeds the configured maximum"
+ + " (" + pSizeMax + ")",
+ pCount, pSizeMax);
+ throw new FileUploadIOException(ex);
+ }
+ };
+ } else {
+ if (sizeMax >= 0 && requestSize > sizeMax) {
+ throw new SizeLimitExceededException(
+ "the request was rejected because its size ("
+ + requestSize
+ + ") exceeds the configured maximum ("
+ + sizeMax + ")",
+ requestSize, sizeMax);
+ }
+ }
}
String charEncoding = headerEncoding;
if (charEncoding == null) {
- charEncoding = ctx.getCharacterEncoding();
+ charEncoding = ctx.getCharacterEncoding();
}
boundary = getBoundary(contentType);
if (boundary == null) {
- throw new FileUploadException(
- "the request was rejected because "
- + "no multipart boundary was found");
+ throw new FileUploadException(
+ "the request was rejected because "
+ + "no multipart boundary was found");
}
- notifier = new MultipartStream.ProgressNotifier(listener, ctx.getContentLength());
+ notifier = new MultipartStream.ProgressNotifier(listener,
+ ctx.getContentLength());
multi = new MultipartStream(input, boundary, notifier);
multi.setHeaderEncoding(charEncoding);
@@ -677,6 +787,11 @@
findNextItem();
}
+ /**
+ * Called for finding the nex item, if any.
+ * @return True, if an next item was found, otherwise false.
+ * @throws IOException An I/O error occurred.
+ */
private boolean findNextItem() throws IOException {
if (eof) {
return false;
@@ -708,9 +823,11 @@
// We're parsing the outer multipart
String fieldName = getFieldName(headers);
if (fieldName != null) {
- String subContentType = getHeader(headers, CONTENT_TYPE);
- if (subContentType != null &&
- subContentType.toLowerCase().startsWith(MULTIPART_MIXED)) {
+ String subContentType
+ = getHeader(headers, CONTENT_TYPE);
+ if (subContentType != null
+ && subContentType.toLowerCase()
+ .startsWith(MULTIPART_MIXED)) {
currentFieldName = fieldName;
// Multiple files associated with this field name
byte[] subBoundary = getBoundary(subContentType);
@@ -720,8 +837,8 @@
}
String fileName = getFileName(headers);
currentItem = new FileItemStreamImpl(fileName,
- fieldName, getHeader(headers, CONTENT_TYPE),
- fileName == null);
+ fieldName, getHeader(headers, CONTENT_TYPE),
+ fileName == null);
notifier.noteItem();
itemValid = true;
return true;
@@ -730,7 +847,8 @@
String fileName = getFileName(headers);
if (fileName != null) {
currentItem = new FileItemStreamImpl(fileName,
- currentFieldName, getHeader(headers, CONTENT_TYPE),
+ currentFieldName,
+ getHeader(headers, CONTENT_TYPE),
false);
notifier.noteItem();
itemValid = true;
@@ -741,6 +859,15 @@
}
}
+ /**
+ * Returns, whether another instance of {@link FileItemStream}
+ * is available.
+ * @throws FileUploadException Parsing or processing the
+ * file item failed.
+ * @throws IOException Reading the file item failed.
+ * @return True, if one or more additional file items
+ * are available, otherwise false.
+ */
public boolean hasNext() throws FileUploadException, IOException {
if (eof) {
return false;
@@ -751,6 +878,16 @@
return findNextItem();
}
+ /**
+ * Returns the next available {@link FileItemStream}.
+ * @throws java.util.NoSuchElementException No more items are
+ * available. Use {@link #hasNext()} to prevent this exception.
+ * @throws FileUploadException Parsing or processing the
+ * file item failed.
+ * @throws IOException Reading the file item failed.
+ * @return FileItemStream instance, which provides
+ * access to the next file item.
+ */
public FileItemStream next() throws FileUploadException, IOException {
if (eof || (!itemValid && !hasNext())) {
throw new NoSuchElementException();
@@ -761,22 +898,33 @@
}
/**
- * This exception is thrown for hiding an inner
+ * This exception is thrown for hiding an inner
* {@link FileUploadException} in an {@link IOException}.
*/
public static class FileUploadIOException extends IOException {
+ /** The exceptions UID, for serializing an instance.
+ */
private static final long serialVersionUID = -7047616958165584154L;
+ /** The exceptions cause; we overwrite the parent
+ * classes field, which is available since Java
+ * 1.4 only.
+ */
private final FileUploadException cause;
/**
* Creates a <code>FileUploadIOException</code> with the
* given cause.
+ * @param pCause The exceptions cause, if any, or null.
*/
public FileUploadIOException(FileUploadException pCause) {
// We're not doing super(pCause) cause of 1.3 compatibility.
cause = pCause;
}
+ /**
+ * Returns the exceptions cause.
+ * @return The exceptions cause, if any, or null.
+ */
public Throwable getCause() {
return cause;
}
@@ -786,7 +934,9 @@
* Thrown to indicate that the request is not a multipart request.
*/
public static class InvalidContentTypeException
- extends FileUploadException {
+ extends FileUploadException {
+ /** The exceptions UID, for serializing an instance.
+ */
private static final long serialVersionUID = -9073026332015646668L;
/**
@@ -794,7 +944,7 @@
* detail message.
*/
public InvalidContentTypeException() {
- super();
+ // Nothing to do.
}
/**
@@ -812,22 +962,37 @@
* Thrown to indicate an IOException.
*/
public static class IOFileUploadException extends FileUploadException {
- private static final long serialVersionUID = 1749796615868477269L;
- private final IOException cause;
+ /** The exceptions UID, for serializing an instance.
+ */
+ private static final long serialVersionUID = 1749796615868477269L;
+ /** The exceptions cause; we overwrite the parent
+ * classes field, which is available since Java
+ * 1.4 only.
+ */
+ private final IOException cause;
- /**
- * Creates a new instance with the given cause.
- */
- public IOFileUploadException(String pMsg, IOException pException) {
- super(pMsg);
- cause = pException;
- }
-
- public Throwable getCause() {
- return cause;
- }
+ /**
+ * Creates a new instance with the given cause.
+ * @param pMsg The detail message.
+ * @param pException The exceptions cause.
+ */
+ public IOFileUploadException(String pMsg, IOException pException) {
+ super(pMsg);
+ cause = pException;
+ }
+
+ /**
+ * Returns the exceptions cause.
+ * @return The exceptions cause, if any, or null.
+ */
+ public Throwable getCause() {
+ return cause;
+ }
}
+ /** This exception is thrown, if a requests permitted size
+ * is exceeded.
+ */
protected abstract static class SizeException extends FileUploadException {
/**
* The actual size of the request.
@@ -839,6 +1004,12 @@
*/
private final long permitted;
+ /**
+ * Creates a new instance.
+ * @param message The detail message.
+ * @param actual The actual number of bytes in the request.
+ * @param permitted The requests size limit, in bytes.
+ */
protected SizeException(String message, long actual, long permitted) {
super(message);
this.actual = actual;
@@ -868,7 +1039,9 @@
* Thrown to indicate that the request size exceeds the configured maximum.
*/
public static class SizeLimitExceededException
- extends SizeException {
+ extends SizeException {
+ /** The exceptions UID, for serializing an instance.
+ */
private static final long serialVersionUID = -2474893167098052828L;
/**
@@ -881,7 +1054,7 @@
*/
public SizeLimitExceededException(String message, long actual,
long permitted) {
- super(message, actual, permitted);
+ super(message, actual, permitted);
}
}
@@ -889,10 +1062,12 @@
* Thrown to indicate that A files size exceeds the configured maximum.
*/
public static class FileSizeLimitExceededException
- extends SizeException {
- private static final long serialVersionUID = 8150776562029630058L;
+ extends SizeException {
+ /** The exceptions UID, for serializing an instance.
+ */
+ private static final long serialVersionUID = 8150776562029630058L;
- /**
+ /**
* Constructs a <code>SizeExceededException</code> with
* the specified detail message, and actual and permitted sizes.
*
@@ -907,16 +1082,18 @@
}
/**
- * Returns the progress listener, if any. Defaults to null.
+ * Returns the progress listener.
+ * @return The progress listener, if any, or null.
*/
- public ProgressListener getProgressListener() {
- return listener;
- }
+ public ProgressListener getProgressListener() {
+ return listener;
+ }
/**
- * Sets the progress listener, if any. Defaults to null.
+ * Sets the progress listener.
+ * @param pListener The progress listener, if any. Defaults to null.
*/
- public void setProgressListener(ProgressListener pListener) {
- listener = pListener;
- }
+ public void setProgressListener(ProgressListener pListener) {
+ listener = pListener;
+ }
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/FileUploadException.java Sun Nov 26 17:06:53 2006
@@ -19,6 +19,7 @@
import java.io.PrintStream;
import java.io.PrintWriter;
+
/**
* Exception for errors encountered while processing the request.
*
@@ -26,14 +27,22 @@
* @version $Id$
*/
public class FileUploadException extends Exception {
- private static final long serialVersionUID = 8881893724388807504L;
- private final Throwable cause;
+ /**
+ * Serial version UID, being used, if the exception
+ * is serialized.
+ */
+ private static final long serialVersionUID = 8881893724388807504L;
+ /**
+ * The exceptions cause. We overwrite the cause of
+ * the super class, which isn't available in Java 1.3.
+ */
+ private final Throwable cause;
/**
* Constructs a new <code>FileUploadException</code> without message.
*/
public FileUploadException() {
- this(null, null);
+ this(null, null);
}
/**
@@ -43,31 +52,44 @@
* @param msg the error message.
*/
public FileUploadException(final String msg) {
- this(msg, null);
+ this(msg, null);
}
/**
* Creates a new <code>FileUploadException</code> with the given
* detail message and cause.
+ * @param msg The exceptions detail message.
+ * @param cause The exceptions cause.
*/
public FileUploadException(String msg, Throwable cause) {
- super(msg);
- this.cause = cause;
+ super(msg);
+ this.cause = cause;
}
+ /**
+ * Prints this throwable and its backtrace to the specified print stream.
+ *
+ * @param stream <code>PrintStream</code> to use for output
+ */
public void printStackTrace(PrintStream stream) {
- super.printStackTrace(stream);
- if (cause != null) {
- stream.println("Caused by:");
- cause.printStackTrace(stream);
- }
+ super.printStackTrace(stream);
+ if (cause != null) {
+ stream.println("Caused by:");
+ cause.printStackTrace(stream);
+ }
}
+ /**
+ * Prints this throwable and its backtrace to the specified
+ * print writer.
+ *
+ * @param writer <code>PrintWriter</code> to use for output
+ */
public void printStackTrace(PrintWriter writer) {
- super.printStackTrace(writer);
- if (cause != null) {
- writer.println("Caused by:");
- cause.printStackTrace(writer);
- }
+ super.printStackTrace(writer);
+ if (cause != null) {
+ writer.println("Caused by:");
+ cause.printStackTrace(writer);
+ }
}
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/MultipartStream.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/MultipartStream.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/MultipartStream.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/MultipartStream.java Sun Nov 26 17:06:53 2006
@@ -87,32 +87,56 @@
* @version $Id$
*/
class MultipartStream {
- static class ProgressNotifier {
- private final ProgressListener listener;
- private final long contentLength;
- private long bytesRead;
- private int items;
- ProgressNotifier(ProgressListener pListener, long pContentLength) {
- listener = pListener;
- contentLength = pContentLength;
- }
- void noteBytesRead(int pBytes) {
- /* Indicates, that the given number of bytes have been read from
- * the input stream.
- */
- bytesRead += pBytes;
- notifyListener();
- }
- void noteItem() {
- ++items;
- }
- private void notifyListener() {
- if (listener != null) {
- listener.update(bytesRead, contentLength, items);
- }
- }
- }
-
+ /**
+ * 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
@@ -152,7 +176,7 @@
* (<code>CRLFCRLF</code>).
*/
protected static final byte[] HEADER_SEPARATOR = {
- CR, LF, CR, LF };
+ CR, LF, CR, LF };
/**
@@ -160,7 +184,7 @@
* followed by an encapsulation (<code>CRLF</code>).
*/
protected static final byte[] FIELD_SEPARATOR = {
- CR, LF};
+ CR, LF};
/**
@@ -168,14 +192,14 @@
* encapsulation in the stream (<code>--</code>).
*/
protected static final byte[] STREAM_TERMINATOR = {
- DASH, DASH};
+ DASH, DASH};
/**
* A byte sequence that precedes a boundary (<code>CRLF--</code>).
*/
protected static final byte[] BOUNDARY_PREFIX = {
- CR, LF, DASH, DASH};
+ CR, LF, DASH, DASH};
/**
@@ -246,7 +270,7 @@
*/
private String headerEncoding;
-
+
/**
* The progress notifier, if any, or null.
*/
@@ -267,14 +291,15 @@
* @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[], ProgressNotifier)
*/
- public MultipartStream(InputStream input,
- byte[] boundary,
- int bufSize,
- ProgressNotifier pNotifier) {
+ MultipartStream(InputStream input,
+ byte[] boundary,
+ int bufSize,
+ ProgressNotifier pNotifier) {
this.input = input;
this.bufSize = bufSize;
this.buffer = new byte[bufSize];
@@ -301,15 +326,14 @@
* @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.
*
- * @throws IOException when an error occurs.
*
* @see #MultipartStream(InputStream, byte[], int, ProgressNotifier)
*/
- public MultipartStream(InputStream input,
- byte[] boundary,
- ProgressNotifier pNotifier)
- throws IOException {
+ MultipartStream(InputStream input,
+ byte[] boundary,
+ ProgressNotifier pNotifier) {
this(input, boundary, DEFAULT_BUFSIZE, pNotifier);
}
@@ -351,7 +375,7 @@
* @throws IOException if there is no more data available.
*/
public byte readByte()
- throws IOException {
+ throws IOException {
// Buffer depleted ?
if (head == tail) {
head = 0;
@@ -360,9 +384,8 @@
if (tail == -1) {
// No more data available.
throw new IOException("No more data is available");
- } else {
- notifier.noteBytesRead(tail);
}
+ notifier.noteBytesRead(tail);
}
return buffer[head++];
}
@@ -379,7 +402,7 @@
* fails to follow required syntax.
*/
public boolean readBoundary()
- throws MalformedStreamException {
+ throws MalformedStreamException {
byte[] marker = new byte[2];
boolean nextChunk = false;
@@ -403,7 +426,7 @@
nextChunk = true;
} else {
throw new MalformedStreamException(
- "Unexpected characters follow a boundary");
+ "Unexpected characters follow a boundary");
}
} catch (IOException e) {
throw new MalformedStreamException("Stream ended unexpectedly");
@@ -432,10 +455,10 @@
* being currently parsed.
*/
public void setBoundary(byte[] boundary)
- throws IllegalBoundaryException {
+ throws IllegalBoundaryException {
if (boundary.length != boundaryLength - BOUNDARY_PREFIX.length) {
throw new IllegalBoundaryException(
- "The length of a boundary token can not be changed");
+ "The length of a boundary token can not be changed");
}
System.arraycopy(boundary, 0, this.boundary, BOUNDARY_PREFIX.length,
boundary.length);
@@ -458,7 +481,7 @@
* @throws MalformedStreamException if the stream ends unexpecetedly.
*/
public String readHeaders()
- throws MalformedStreamException {
+ throws MalformedStreamException {
int i = 0;
byte[] b = new byte[1];
// to support multi-byte characters
@@ -518,12 +541,15 @@
* @throws IOException if an i/o error occurs.
*/
public int readBodyData(OutputStream output)
- throws MalformedStreamException,
- IOException {
+ throws MalformedStreamException, IOException {
final InputStream istream = newInputStream();
return (int) Streams.copy(istream, output, false);
}
+ /**
+ * Creates a new {@link ItemInputStream}.
+ * @return A new instance of {@link ItemInputStream}.
+ */
ItemInputStream newInputStream() {
return new ItemInputStream();
}
@@ -541,8 +567,8 @@
* @throws IOException if an i/o error occurs.
*/
public int discardBodyData()
- throws MalformedStreamException,
- IOException {
+ throws MalformedStreamException,
+ IOException {
return readBodyData(null);
}
@@ -556,7 +582,7 @@
* @throws IOException if an i/o error occurs.
*/
public boolean skipPreamble()
- throws IOException {
+ throws IOException {
// First delimiter may be not preceeded with a CRLF.
System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);
boundaryLength = boundary.length - 2;
@@ -591,8 +617,8 @@
* <code>a</code> and <code>b</code> are equal.
*/
public static boolean arrayequals(byte[] a,
- byte[] b,
- int count) {
+ byte[] b,
+ int count) {
for (int i = 0; i < count; i++) {
if (a[i] != b[i]) {
return false;
@@ -613,7 +639,7 @@
* <code>buffer</code>, or <code>-1</code> if not found.
*/
protected int findByte(byte value,
- int pos) {
+ int pos) {
for (int i = pos; i < tail; i++) {
if (buffer[i] == value) {
return i;
@@ -637,8 +663,8 @@
int match = 0;
int maxpos = tail - boundaryLength;
for (first = head;
- (first <= maxpos) && (match != boundaryLength);
- first++) {
+ (first <= maxpos) && (match != boundaryLength);
+ first++) {
first = findByte(boundary[0], first);
if (first == -1 || (first > maxpos)) {
return -1;
@@ -660,7 +686,7 @@
* required syntax.
*/
public static class MalformedStreamException
- extends IOException {
+ extends IOException {
/**
* Constructs a <code>MalformedStreamException</code> with no
* detail message.
@@ -685,7 +711,7 @@
* Thrown upon attempt of setting an invalid boundary token.
*/
public static class IllegalBoundaryException
- extends IOException {
+ extends IOException {
/**
* Constructs an <code>IllegalBoundaryException</code> with no
* detail message.
@@ -709,14 +735,30 @@
* An {@link 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;
- private int pad, pos;
+ /** 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() {
pos = MultipartStream.this.findSeparator();
if (pos == -1) {
@@ -728,13 +770,21 @@
}
}
- /** Returns the number of bytes, which have been read
+ /**
+ * Returns the number of bytes, which have been read
* by the stream.
+ * @return Number of bytes, which have been read so far.
*/
public long getBytesRead() {
return total;
}
+ /**
+ * Returns the number of bytes, which are currently
+ * available, without blocking.
+ * @throws IOException An I/O error occurs.
+ * @return Number of bytes in the buffer.
+ */
public int available() throws IOException {
if (pos == -1) {
return tail - head - pad;
@@ -742,6 +792,16 @@
return pos - head;
}
+ /** Offset when converting negative bytes to integers.
+ */
+ private static final int BYTE_POSITIVE_OFFSET = 256;
+
+ /**
+ * Returns the next byte in the stream.
+ * @return The next byte in the stream, as a non-negative
+ * integer, or -1 for EOF.
+ * @throws IOException An I/O error occurred.
+ */
public int read() throws IOException {
if (closed) {
throw new FileItemStream.ItemSkippedException();
@@ -753,9 +813,21 @@
}
++total;
int b = buffer[head++];
- return b >= 0 ? b : b + 256;
+ if (b >= 0) {
+ return b;
+ }
+ return b + BYTE_POSITIVE_OFFSET;
}
+ /**
+ * Reads bytes into the given buffer.
+ * @param b The destination buffer, where to write to.
+ * @param off Offset of the first byte in the buffer.
+ * @param len Maximum number of bytes to read.
+ * @return Number of bytes, which have been actually read,
+ * or -1 for EOF.
+ * @throws IOException An I/O error occurred.
+ */
public int read(byte[] b, int off, int len) throws IOException {
if (closed) {
throw new FileItemStream.ItemSkippedException();
@@ -777,6 +849,10 @@
return res;
}
+ /**
+ * Closes the input stream.
+ * @throws IOException An I/O error occurred.
+ */
public void close() throws IOException {
if (closed) {
return;
@@ -794,6 +870,13 @@
closed = true;
}
+ /**
+ * Skips the given number of bytes.
+ * @param bytes Number of bytes to skip.
+ * @return The number of bytes, which have actually been
+ * skipped.
+ * @throws IOException An I/O error occurred.
+ */
public long skip(long bytes) throws IOException {
if (closed) {
throw new FileItemStream.ItemSkippedException();
@@ -810,6 +893,11 @@
return res;
}
+ /**
+ * Attempts to read more data.
+ * @return Number of available bytes
+ * @throws IOException An I/O error occurred.
+ */
private int makeAvailable() throws IOException {
if (pos != -1) {
return 0;
@@ -827,16 +915,17 @@
// Boundary can't be in there so signal an error
// condition.
throw new MalformedStreamException(
- "Stream ended unexpectedly");
- } else {
- notifier.noteBytesRead(bytesRead);
+ "Stream ended unexpectedly");
}
+ notifier.noteBytesRead(bytesRead);
tail = pad + bytesRead;
findSeparator();
return available();
}
- /** Returns, whether the stream is closed.
+ /**
+ * Returns, whether the stream is closed.
+ * @return True, if the stream is closed, otherwise false.
*/
public boolean isClosed() {
return closed;
@@ -907,5 +996,5 @@
}
}
- */
+ */
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/disk/DiskFileItem.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/disk/DiskFileItem.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/disk/DiskFileItem.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/disk/DiskFileItem.java Sun Nov 26 17:06:53 2006
@@ -28,7 +28,6 @@
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.UnsupportedEncodingException;
-import java.rmi.server.UID;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.FileCleaner;
@@ -85,7 +84,8 @@
* UID used in unique file name generation.
*/
private static final String UID =
- new UID().toString().replace(':', '_').replace('-', '_');
+ new java.rmi.server.UID().toString()
+ .replace(':', '_').replace('-', '_');
/**
* Counter used in unique identifier generation.
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/portlet/PortletFileUpload.java Sun Nov 26 17:06:53 2006
@@ -20,7 +20,6 @@
import java.util.List;
import javax.portlet.ActionRequest;
-import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
@@ -34,9 +33,9 @@
* <p>This class handles multiple files per single HTML widget, sent using
* <code>multipart/mixed</code> encoding type, as specified by
* <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
- * #parseRequest(HttpServletRequest)} to acquire a list of {@link
- * org.apache.commons.fileupload.FileItem}s associated with a given HTML
- * widget.</p>
+ * #parseRequest(javax.servlet.http.HttpServletRequest)} to acquire a list
+ * of {@link org.apache.commons.fileupload.FileItem FileItems} associated
+ * with a given HTML widget.</p>
*
* <p>How the data for individual parts is stored is determined by the factory
* used to create them; a given part may be in memory, on disk, or somewhere
@@ -93,6 +92,7 @@
* create <code>FileItem</code> instances.
*
* @see FileUpload#FileUpload()
+ * @param fileItemFactory The factory to use for creating file items.
*/
public PortletFileUpload(FileItemFactory fileItemFactory) {
super(fileItemFactory);
@@ -135,7 +135,8 @@
* error while communicating with the client or a problem while
* storing the uploaded content.
*/
- public FileItemIterator getItemIterator(ActionRequest request) throws FileUploadException, IOException {
+ public FileItemIterator getItemIterator(ActionRequest request)
+ throws FileUploadException, IOException {
return super.getItemIterator(new PortletRequestContext(request));
}
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/servlet/ServletFileUpload.java Sun Nov 26 17:06:53 2006
@@ -63,7 +63,7 @@
* @return <code>true</code> if the request is multipart;
* <code>false</code> otherwise.
*/
- public static final boolean isMultipartContent(
+ public static final boolean isMultipartContent(
HttpServletRequest request) {
if (!"post".equals(request.getMethod().toLowerCase())) {
return false;
@@ -99,6 +99,7 @@
* create <code>FileItem</code> instances.
*
* @see FileUpload#FileUpload()
+ * @param fileItemFactory The factory to use for creating file items.
*/
public ServletFileUpload(FileItemFactory fileItemFactory) {
super(fileItemFactory);
@@ -121,7 +122,7 @@
* the request or storing files.
*/
public List /* FileItem */ parseRequest(HttpServletRequest request)
- throws FileUploadException {
+ throws FileUploadException {
return parseRequest(new ServletRequestContext(request));
}
@@ -143,7 +144,7 @@
* storing the uploaded content.
*/
public FileItemIterator getItemIterator(HttpServletRequest request)
- throws FileUploadException, IOException {
+ throws FileUploadException, IOException {
return super.getItemIterator(new ServletRequestContext(request));
}
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Closeable.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Closeable.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Closeable.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Closeable.java Sun Nov 26 17:06:53 2006
@@ -23,13 +23,16 @@
* Interface of an object, which may be closed.
*/
public interface Closeable {
- /**
- * Closes the object.
- */
- public void close() throws IOException;
+ /**
+ * Closes the object.
+ * @throws IOException An I/O error occurred.
+ */
+ void close() throws IOException;
- /**
- * Returns, whether the object is already closed.
- */
- public boolean isClosed() throws IOException;
+ /**
+ * Returns, whether the object is already closed.
+ * @return True, if the object is closed, otherwise false.
+ * @throws IOException An I/O error occurred.
+ */
+ boolean isClosed() throws IOException;
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/LimitedInputStream.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/LimitedInputStream.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/LimitedInputStream.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/LimitedInputStream.java Sun Nov 26 17:06:53 2006
@@ -25,55 +25,131 @@
* An input stream, which limits its data size. This stream is
* used, if the content length is unknown.
*/
-public abstract class LimitedInputStream extends FilterInputStream
- implements Closeable {
- private long sizeMax;
- private long count;
- private boolean closed;
-
- /**
- * Creates a new instance.
- * @param pIn The input stream, which shall be limited.
- * @param pSizeMax The limit; no more than this number of bytes
- * shall be returned by the source stream.
- */
- public LimitedInputStream(InputStream pIn, long pSizeMax) {
- super(pIn);
- sizeMax = pSizeMax;
- }
-
- protected abstract void raiseError(long pSizeMax, long pCount) throws IOException;
-
- private void checkLimit() throws IOException {
- if (count > sizeMax) {
- raiseError(sizeMax, count);
- }
- }
-
- public int read() throws IOException {
- int res = super.read();
- if (res != -1) {
- count++;
- checkLimit();
- }
- return res;
- }
-
- public int read(byte[] b, int off, int len) throws IOException {
- int res = super.read(b, off, len);
- if (res > 0) {
- count += res;
- checkLimit();
- }
- return res;
- }
-
- public boolean isClosed() throws IOException {
- return closed;
- }
-
- public void close() throws IOException {
- closed = true;
- super.close();
- }
+public abstract class LimitedInputStream
+ extends FilterInputStream implements Closeable {
+ /**
+ * The maximum size of an item, in bytes.
+ */
+ private long sizeMax;
+ /**
+ * The current number of bytes.
+ */
+ private long count;
+ /**
+ * Whether this stream is already closed.
+ */
+ private boolean closed;
+
+ /**
+ * Creates a new instance.
+ * @param pIn The input stream, which shall be limited.
+ * @param pSizeMax The limit; no more than this number of bytes
+ * shall be returned by the source stream.
+ */
+ public LimitedInputStream(InputStream pIn, long pSizeMax) {
+ super(pIn);
+ sizeMax = pSizeMax;
+ }
+
+ /**
+ * Called to indicate, that the input streams limit has
+ * been exceeded.
+ * @param pSizeMax The input streams limit, in bytes.
+ * @param pCount The actual number of bytes.
+ * @throws IOException The called method is expected
+ * to raise an IOException.
+ */
+ protected abstract void raiseError(long pSizeMax, long pCount)
+ throws IOException;
+
+ /** Called to check, whether the input streams
+ * limit is reached.
+ * @throws IOException The given limit is exceeded.
+ */
+ private void checkLimit() throws IOException {
+ if (count > sizeMax) {
+ raiseError(sizeMax, count);
+ }
+ }
+
+ /**
+ * Reads the next byte of data from this input stream. The value
+ * byte is returned as an <code>int</code> in the range
+ * <code>0</code> to <code>255</code>. If no byte is available
+ * because the end of the stream has been reached, the value
+ * <code>-1</code> is returned. This method blocks until input data
+ * is available, the end of the stream is detected, or an exception
+ * is thrown.
+ * <p>
+ * This method
+ * simply performs <code>in.read()</code> and returns the result.
+ *
+ * @return the next byte of data, or <code>-1</code> if the end of the
+ * stream is reached.
+ * @exception IOException if an I/O error occurs.
+ * @see java.io.FilterInputStream#in
+ */
+ public int read() throws IOException {
+ int res = super.read();
+ if (res != -1) {
+ count++;
+ checkLimit();
+ }
+ return res;
+ }
+
+ /**
+ * Reads up to <code>len</code> bytes of data from this input stream
+ * into an array of bytes. If <code>len</code> is not zero, the method
+ * blocks until some input is available; otherwise, no
+ * bytes are read and <code>0</code> is returned.
+ * <p>
+ * This method simply performs <code>in.read(b, off, len)</code>
+ * and returns the result.
+ *
+ * @param b the buffer into which the data is read.
+ * @param off The start offset in the destination array
+ * <code>b</code>.
+ * @param len the maximum number of bytes read.
+ * @return the total number of bytes read into the buffer, or
+ * <code>-1</code> if there is no more data because the end of
+ * the stream has been reached.
+ * @exception NullPointerException If <code>b</code> is <code>null</code>.
+ * @exception IndexOutOfBoundsException If <code>off</code> is negative,
+ * <code>len</code> is negative, or <code>len</code> is greater than
+ * <code>b.length - off</code>
+ * @exception IOException if an I/O error occurs.
+ * @see java.io.FilterInputStream#in
+ */
+ public int read(byte[] b, int off, int len) throws IOException {
+ int res = super.read(b, off, len);
+ if (res > 0) {
+ count += res;
+ checkLimit();
+ }
+ return res;
+ }
+
+ /**
+ * Returns, whether this stream is already closed.
+ * @return True, if the stream is closed, otherwise false.
+ * @throws IOException An I/O error occurred.
+ */
+ public boolean isClosed() throws IOException {
+ return closed;
+ }
+
+ /**
+ * Closes this input stream and releases any system resources
+ * associated with the stream.
+ * This
+ * method simply performs <code>in.close()</code>.
+ *
+ * @exception IOException if an I/O error occurs.
+ * @see java.io.FilterInputStream#in
+ */
+ public void close() throws IOException {
+ closed = true;
+ super.close();
+ }
}
Modified: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Streams.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Streams.java?view=diff&rev=479484&r1=479483&r2=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Streams.java (original)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/Streams.java Sun Nov 26 17:06:53 2006
@@ -21,12 +21,24 @@
import java.io.InputStream;
import java.io.OutputStream;
-import org.apache.commons.fileupload.FileItemStream;
-
/** Utility class for working with streams.
*/
-public class Streams {
+public final class Streams {
+ /**
+ * Private constructor, to prevent instantiation.
+ * This class has only static methods.
+ */
+ private Streams() {
+ // Does nothing
+ }
+
+ /**
+ * Default buffer size for use in
+ * {@link #copy(InputStream, OutputStream, boolean)}.
+ */
+ private static final int DEFAULT_BUFFER_SIZE = 8192;
+
/**
* Copies the contents of the given {@link InputStream}
* to the given {@link OutputStream}. Shortcut for
@@ -42,21 +54,20 @@
* @param pClose True guarantees, that {@link OutputStream#close()}
* is called on the stream. False indicates, that only
* {@link OutputStream#flush()} should be called finally.
- *
+ *
* @return Number of bytes, which have been copied.
+ * @throws IOException An I/O error occurred.
*/
public static long copy(InputStream pInputStream,
- OutputStream pOutputStream, boolean pClose)
+ OutputStream pOutputStream, boolean pClose)
throws IOException {
- return copy(pInputStream, pOutputStream, pClose, new byte[8192]);
+ return copy(pInputStream, pOutputStream, pClose,
+ new byte[DEFAULT_BUFFER_SIZE]);
}
/**
* Copies the contents of the given {@link InputStream}
- * to the given {@link OutputStream}. Shortcut for
- * <pre>
- * copy(pInputStream, pOutputStream, new byte[8192]);
- * </pre>
+ * to the given {@link OutputStream}.
* @param pIn The input stream, which is being read.
* It is guaranteed, that {@link InputStream#close()} is called
* on the stream.
@@ -69,14 +80,15 @@
* @param pBuffer Temporary buffer, which is to be used for
* copying data.
* @return Number of bytes, which have been copied.
+ * @throws IOException An I/O error occurred.
*/
public static long copy(InputStream pIn,
- OutputStream pOut, boolean pClose,
- byte[] pBuffer)
- throws IOException {
- OutputStream out = pOut;
- InputStream in = pIn;
- try {
+ OutputStream pOut, boolean pClose,
+ byte[] pBuffer)
+ throws IOException {
+ OutputStream out = pOut;
+ InputStream in = pIn;
+ try {
long total = 0;
for (;;) {
int res = in.read(pBuffer);
@@ -120,11 +132,14 @@
}
/**
- * This convenience method allows to read a {@link FileItemStream}'s
+ * This convenience method allows to read a
+ * {@link org.apache.commons.fileupload.FileItemStream}'s
* content into a string. The platform's default character encoding
* is used for converting bytes into characters.
* @param pStream The input stream to read.
* @see #asString(InputStream, String)
+ * @return The streams contents, as a string.
+ * @throws IOException An I/O error occurred.
*/
public static String asString(InputStream pStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -133,11 +148,14 @@
}
/**
- * This convenience method allows to read a {@link FileItemStream}'s
+ * This convenience method allows to read a
+ * {@link org.apache.commons.fileupload.FileItemStream}'s
* content into a string, using the given character encoding.
* @param pStream The input stream to read.
* @param pEncoding The character encoding, typically "UTF-8".
* @see #asString(InputStream)
+ * @return The streams contents, as a string.
+ * @throws IOException An I/O error occurred.
*/
public static String asString(InputStream pStream, String pEncoding)
throws IOException {
Added: jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/package.html
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/package.html?view=auto&rev=479484
==============================================================================
--- jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/package.html (added)
+++ jakarta/commons/proper/fileupload/trunk/src/java/org/apache/commons/fileupload/util/package.html Sun Nov 26 17:06:53 2006
@@ -0,0 +1,29 @@
+<!--
+ 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.
+-->
+<!-- $Id: package.html 479262 2006-11-26 03:09:24Z niallp $ -->
+<html>
+ <head>
+ <title>Overview of the org.apache.commons.fileupload.util component</title>
+ </head>
+ <body>
+ <p>
+ This package contains various IO related utility classes
+ or methods, which are basically reusable and not necessarily
+ restricted to the scope of a file upload.
+ </p>
+ </body>
+</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org