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
The following commit(s) were added to refs/heads/master by this push:
new f0c1150 Use try-with-resources
f0c1150 is described below
commit f0c115078d92c07ef1cf15b04aecdf48b7b7be89
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Apr 2 23:08:44 2023 -0400
Use try-with-resources
Reuse IOUtils.copy() methods
---
.../apache/commons/fileupload2/FileUploadBase.java | 14 ++--
.../commons/fileupload2/MultipartStream.java | 16 +++--
.../apache/commons/fileupload2/util/Streams.java | 84 ----------------------
.../fileupload2/DiskFileItemSerializeTest.java | 2 -
.../org/apache/commons/fileupload2/SizesTest.java | 17 +++--
5 files changed, 27 insertions(+), 106 deletions(-)
diff --git a/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java
b/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java
index 3257ea0..6e90d4f 100644
--- a/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java
+++ b/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java
@@ -19,6 +19,8 @@ package org.apache.commons.fileupload2;
import static java.lang.String.format;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
@@ -30,7 +32,7 @@ import java.util.Objects;
import org.apache.commons.fileupload2.impl.FileItemIteratorImpl;
import org.apache.commons.fileupload2.pub.FileUploadFileCountLimitException;
import org.apache.commons.fileupload2.util.FileItemHeadersImpl;
-import org.apache.commons.fileupload2.util.Streams;
+import org.apache.commons.io.IOUtils;
/**
* <p>High level API for processing file uploads.</p>
@@ -459,7 +461,7 @@ public abstract class FileUploadBase {
try {
final FileItemIterator iter = getItemIterator(ctx);
final FileItemFactory fileItemFactory =
Objects.requireNonNull(getFileItemFactory(), "No FileItemFactory has been
set.");
- final byte[] buffer = new byte[Streams.DEFAULT_BUFFER_SIZE];
+ final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
while (iter.hasNext()) {
if (items.size() == fileCountMax) {
// The next item will exceed the limit.
@@ -470,15 +472,15 @@ public abstract class FileUploadBase {
final String fileName = item.getName();
final FileItem fileItem =
fileItemFactory.createItem(item.getFieldName(), item.getContentType(),
item.isFormField(), fileName);
items.add(fileItem);
- try {
- Streams.copy(item.openStream(),
fileItem.getOutputStream(), true, buffer);
+ try (InputStream inputStream = item.openStream();
+ OutputStream outputStream =
fileItem.getOutputStream()) {
+ IOUtils.copyLarge(inputStream, outputStream, buffer);
} catch (final FileUploadException e) {
throw e;
} catch (final IOException e) {
throw new FileUploadException(format("Processing of %s
request failed. %s", MULTIPART_FORM_DATA, e.getMessage()), e);
}
- final FileItemHeaders fih = item.getHeaders();
- fileItem.setHeaders(fih);
+ fileItem.setHeaders(item.getHeaders());
}
successful = true;
return items;
diff --git a/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
b/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
index ea9e417..9acea9f 100644
--- a/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
+++ b/src/main/java/org/apache/commons/fileupload2/MultipartStream.java
@@ -25,7 +25,8 @@ import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.fileupload2.pub.FileUploadSizeException;
-import org.apache.commons.fileupload2.util.Streams;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.NullOutputStream;
/**
* <p>
@@ -654,8 +655,8 @@ public class MultipartStream {
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
- public int discardBodyData() throws MalformedStreamException, IOException {
- return readBodyData(null);
+ public long discardBodyData() throws MalformedStreamException, IOException
{
+ return readBodyData(NullOutputStream.NULL_OUTPUT_STREAM);
}
/**
@@ -733,8 +734,10 @@ public class MultipartStream {
* @throws MalformedStreamException if the stream ends unexpectedly.
* @throws IOException if an i/o error occurs.
*/
- public int readBodyData(final OutputStream output) throws
MalformedStreamException, IOException {
- return (int) Streams.copy(newInputStream(), output, false); // N.B.
Streams.copy closes the input stream
+ public long readBodyData(final OutputStream output) throws
MalformedStreamException, IOException {
+ try (ItemInputStream inputStream = newInputStream()) {
+ return IOUtils.copyLarge(inputStream, output);
+ }
}
/**
@@ -745,7 +748,7 @@ public class MultipartStream {
* @throws FileUploadSizeException if the bytes read from the stream
exceeded the size limits
* @throws MalformedStreamException if the stream ends unexpectedly or
fails to follow required syntax.
*/
- public boolean readBoundary() throws FileUploadException,
MalformedStreamException {
+ public boolean readBoundary() throws FileUploadSizeException,
MalformedStreamException {
final byte[] marker = new byte[2];
final boolean nextChunk;
@@ -771,7 +774,6 @@ public class MultipartStream {
throw new MalformedStreamException("Unexpected characters
follow a boundary");
}
} catch (final FileUploadSizeException e) {
- // wraps a FileUploadSizeException, re-throw as it will be
unwrapped later?
throw e;
} catch (final IOException e) {
throw new MalformedStreamException("Stream ended unexpectedly", e);
diff --git a/src/main/java/org/apache/commons/fileupload2/util/Streams.java
b/src/main/java/org/apache/commons/fileupload2/util/Streams.java
index 42135ef..e29a77c 100644
--- a/src/main/java/org/apache/commons/fileupload2/util/Streams.java
+++ b/src/main/java/org/apache/commons/fileupload2/util/Streams.java
@@ -16,11 +16,6 @@
*/
package org.apache.commons.fileupload2.util;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
import org.apache.commons.fileupload2.InvalidFileNameException;
/**
@@ -28,12 +23,6 @@ import
org.apache.commons.fileupload2.InvalidFileNameException;
*/
public final class Streams {
- /**
- * Default buffer size for use in
- * {@link #copy(InputStream, OutputStream, boolean)}.
- */
- public static final int DEFAULT_BUFFER_SIZE = 8192;
-
/**
* Checks, whether the given file name is valid in the sense,
* that it doesn't contain any NUL characters. If the file name
@@ -65,79 +54,6 @@ public final class Streams {
return fileName;
}
- /**
- * Copies the contents of the given {@link InputStream}
- * to the given {@link OutputStream}. Shortcut for
- * <pre>
- * copy(pInputStream, pOutputStream, new byte[8192]);
- * </pre>
- *
- * @param inputStream The input stream, which is being read.
- * It is guaranteed, that {@link InputStream#close()} is called
- * on the stream.
- * @param outputStream The output stream, to which data should
- * be written. May be null, in which case the input streams
- * contents are simply discarded.
- * @param closeOutputStream 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(final InputStream inputStream, final OutputStream
outputStream,
- final boolean closeOutputStream)
- throws IOException {
- return copy(inputStream, outputStream, closeOutputStream, new
byte[DEFAULT_BUFFER_SIZE]);
- }
-
- /**
- * Copies the contents of the given {@link InputStream}
- * to the given {@link OutputStream}.
- *
- * @param inputStream The input stream, which is being read.
- * It is guaranteed, that {@link InputStream#close()} is called
- * on the stream.
- * @param outputStream The output stream, to which data should
- * be written. May be null, in which case the input streams
- * contents are simply discarded.
- * @param closeOutputStream True guarantees, that {@link
OutputStream#close()}
- * is called on the stream. False indicates, that only
- * {@link OutputStream#flush()} should be called finally.
- * @param buffer 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(final InputStream inputStream, final OutputStream
outputStream, final boolean closeOutputStream, final byte[] buffer)
- throws IOException {
- try (OutputStream out = outputStream;
- InputStream in = inputStream) {
- long total = 0;
- for (;;) {
- final int res = in.read(buffer);
- if (res == -1) {
- break;
- }
- if (res > 0) {
- total += res;
- if (out != null) {
- out.write(buffer, 0, res);
- }
- }
- }
- if (out != null) {
- if (closeOutputStream) {
- out.close();
- } else {
- out.flush();
- }
- }
- in.close();
- return total;
- }
- }
-
/**
* Private constructor, to prevent instantiation.
* This class has only static methods.
diff --git
a/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
b/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
index 8d913c7..14be63d 100644
---
a/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
+++
b/src/test/java/org/apache/commons/fileupload2/DiskFileItemSerializeTest.java
@@ -23,11 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
-import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
diff --git a/src/test/java/org/apache/commons/fileupload2/SizesTest.java
b/src/test/java/org/apache/commons/fileupload2/SizesTest.java
index 398b87d..3099fd5 100644
--- a/src/test/java/org/apache/commons/fileupload2/SizesTest.java
+++ b/src/test/java/org/apache/commons/fileupload2/SizesTest.java
@@ -34,7 +34,7 @@ import
org.apache.commons.fileupload2.disk.DiskFileItemFactory;
import org.apache.commons.fileupload2.pub.FileUploadByteCountLimitException;
import org.apache.commons.fileupload2.pub.FileUploadSizeException;
import org.apache.commons.fileupload2.servlet.ServletFileUpload;
-import org.apache.commons.fileupload2.util.Streams;
+import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
/**
@@ -259,9 +259,11 @@ public class SizesTest {
assertEquals("foo1.tab", item.getName());
{
- final InputStream stream = item.openStream();
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- Streams.copy(stream, baos, true);
+ try (final ByteArrayOutputStream baos = new
ByteArrayOutputStream();
+ final InputStream stream = item.openStream()) {
+ IOUtils.copy(stream, baos);
+ }
+
}
// the second item is over the size max, thus we expect an error
@@ -275,9 +277,10 @@ public class SizesTest {
item = it.next();
try {
- final InputStream stream = item.openStream();
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- Streams.copy(stream, baos, true);
+ try (final ByteArrayOutputStream baos = new
ByteArrayOutputStream();
+ final InputStream stream = item.openStream()) {
+ IOUtils.copy(stream, baos);
+ }
fail();
} catch (final FileUploadException e) {
// expected