This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
The following commit(s) were added to refs/heads/1.x by this push:
new 6b44674b Use IOUtils.copyLarge()
6b44674b is described below
commit 6b44674b3daf5f976d9dbfd321c0ed0b69820dcd
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sat Mar 1 10:58:18 2025 -0500
Use IOUtils.copyLarge()
---
.../apache/commons/fileupload/util/Streams.java | 38 ++++------------------
1 file changed, 6 insertions(+), 32 deletions(-)
diff --git a/src/main/java/org/apache/commons/fileupload/util/Streams.java
b/src/main/java/org/apache/commons/fileupload/util/Streams.java
index 78d52295..c766951a 100644
--- a/src/main/java/org/apache/commons/fileupload/util/Streams.java
+++ b/src/main/java/org/apache/commons/fileupload/util/Streams.java
@@ -23,6 +23,7 @@ import java.io.OutputStream;
import org.apache.commons.fileupload.InvalidFileNameException;
import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.NullOutputStream;
/**
* Utility class for working with streams.
@@ -144,41 +145,14 @@ public final class Streams {
* @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 {
- OutputStream out = outputStream;
- InputStream in = inputStream;
+ public static long copy(final InputStream inputStream, final OutputStream
outputStream, final boolean closeOutputStream, final byte[] buffer)
+ throws IOException {
try {
- 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();
- }
- out = null;
- }
- in.close();
- in = null;
- return total;
+ return IOUtils.copyLarge(inputStream, outputStream != null ?
outputStream : NullOutputStream.INSTANCE, buffer);
} finally {
- IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(inputStream);
if (closeOutputStream) {
- IOUtils.closeQuietly(out);
+ IOUtils.closeQuietly(outputStream);
}
}
}