You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/11 21:33:16 UTC

[commons-fileupload] 04/04: FileItemInputIterator now extends IOIterator

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit 59c7c13cfb8b9f3d719c601dea6d9f84eb453a2b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jun 11 17:31:16 2023 -0400

    FileItemInputIterator now extends IOIterator<FileItemInput>
---
 .../org/apache/commons/fileupload2/AbstractFileUpload.java   | 12 +++++-------
 .../apache/commons/fileupload2/FileItemInputIterator.java    | 10 +++++++---
 .../commons/fileupload2/FileItemInputIteratorImpl.java       | 11 +++++++++--
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java
index 3b533a7..8097de9 100644
--- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java
+++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/AbstractFileUpload.java
@@ -259,14 +259,14 @@ public abstract class AbstractFileUpload {
     /**
      * Gets an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> compliant {@code multipart/form-data} stream.
      *
-     * @param ctx The context for the request to be parsed.
+     * @param requestContext The context for the request to be parsed.
      * @return An iterator to instances of {@code FileItemInput} parsed from the request, in the order that they were transmitted.
      * @throws FileUploadException if there are problems reading/parsing the request or storing files.
      * @throws IOException         An I/O error occurred. This may be a network error while communicating with the client or a problem while storing the
      *                             uploaded content.
      */
-    public FileItemInputIterator getItemIterator(final RequestContext ctx) throws FileUploadException, IOException {
-        return new FileItemInputIteratorImpl(this, ctx);
+    public FileItemInputIterator getItemIterator(final RequestContext requestContext) throws FileUploadException, IOException {
+        return new FileItemInputIteratorImpl(this, requestContext);
     }
 
     /**
@@ -410,15 +410,13 @@ public abstract class AbstractFileUpload {
         final List<FileItem> itemList = new ArrayList<>();
         boolean successful = false;
         try {
-            final FileItemInputIterator iter = getItemIterator(requestContext);
             final FileItemFactory fileItemFactory = Objects.requireNonNull(getFileItemFactory(), "No FileItemFactory has been set.");
             final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
-            while (iter.hasNext()) {
+            getItemIterator(requestContext).forEachRemaining(fileItemInput -> {
                 if (itemList.size() == fileCountMax) {
                     // The next item will exceed the limit.
                     throw new FileUploadFileCountLimitException(ATTACHMENT, getFileCountMax(), itemList.size());
                 }
-                final FileItemInput fileItemInput = iter.next();
                 // Don't use getName() here to prevent an InvalidFileNameException.
                 final String fileName = fileItemInput.getName();
                 // @formatter:off
@@ -439,7 +437,7 @@ public abstract class AbstractFileUpload {
                 } catch (final IOException e) {
                     throw new FileUploadException(String.format("Processing of %s request failed. %s", MULTIPART_FORM_DATA, e.getMessage()), e);
                 }
-            }
+            });
             successful = true;
             return itemList;
         } catch (final FileUploadException e) {
diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIterator.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIterator.java
index a9ddc31..34d4e73 100644
--- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIterator.java
+++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIterator.java
@@ -21,10 +21,12 @@ import java.util.List;
 
 import javax.naming.SizeLimitExceededException;
 
+import org.apache.commons.io.function.IOIterator;
+
 /**
  * An iterator, as returned by {@link AbstractFileUpload#getItemIterator(RequestContext)}.
  */
-public interface FileItemInputIterator {
+public interface FileItemInputIterator extends IOIterator<FileItemInput> {
 
     List<FileItem> getFileItems() throws FileUploadException, IOException;
 
@@ -53,7 +55,8 @@ public interface FileItemInputIterator {
      * @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;
+    @Override
+    boolean hasNext() throws IOException;
 
     /**
      * Returns the next available {@link FileItemInput}.
@@ -63,7 +66,8 @@ public interface FileItemInputIterator {
      * @throws IOException                      Reading the file item failed.
      * @return FileItemInput instance, which provides access to the next file item.
      */
-    FileItemInput next() throws FileUploadException, IOException;
+    @Override
+    FileItemInput next() throws IOException;
 
     /**
      * Sets the maximum size of a single file. An {@link FileUploadByteCountLimitException} will be thrown, if there is an uploaded file, which is exceeding
diff --git a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIteratorImpl.java b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIteratorImpl.java
index 5156035..b11f760 100644
--- a/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIteratorImpl.java
+++ b/commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/FileItemInputIteratorImpl.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.NoSuchElementException;
@@ -236,7 +237,7 @@ class FileItemInputIteratorImpl implements FileItemInputIterator {
      * @return True, if one or more additional file items are available, otherwise false.
      */
     @Override
-    public boolean hasNext() throws FileUploadException, IOException {
+    public boolean hasNext() throws IOException {
         if (eof) {
             return false;
         }
@@ -305,7 +306,7 @@ class FileItemInputIteratorImpl implements FileItemInputIterator {
      * @return FileItemInput instance, which provides access to the next file item.
      */
     @Override
-    public FileItemInput next() throws FileUploadException, IOException {
+    public FileItemInput next() throws IOException {
         if (eof || !itemValid && !hasNext()) {
             throw new NoSuchElementException();
         }
@@ -323,4 +324,10 @@ class FileItemInputIteratorImpl implements FileItemInputIterator {
         this.sizeMax = sizeMax;
     }
 
+    @Override
+    public Iterator<FileItemInput> unwrap() {
+        // TODO Something better?
+        return (Iterator<FileItemInput>) this;
+    }
+
 }