Skip to content

Commit

Permalink
Use IOUtils.copyLarge()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Mar 1, 2025
1 parent 0d0cbc8 commit 6b44674
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions src/main/java/org/apache/commons/fileupload/util/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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.
Expand Down Expand Up @@ -144,41 +145,14 @@ public static long copy(final InputStream inputStream, final OutputStream output
* @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);
}
}
}
Expand Down

0 comments on commit 6b44674

Please # to comment.