Skip to content

Commit

Permalink
Reuse IOUtils in write(OutputStream, int)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Mar 1, 2025
1 parent 69531f0 commit e1db69f
Showing 1 changed file with 5 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileContentInfo;
Expand Down Expand Up @@ -848,29 +849,18 @@ public long write(final OutputStream output) throws IOException {
}

/**
* Writes this content to an OutputStream.
* Copies this content to an OutputStream.
*
* @param output The target OutputStream.
* @param bufferSize The buffer size to write data chunks.
* @return the total number of bytes written
* @return the total number of bytes written.
* @throws IOException if an error occurs writing the file.
* @since 2.1
*/
@Override
public long write(final OutputStream output, final int bufferSize) throws IOException {
final InputStream input = getInputStream();
long count = 0;
try {
// This read/write code from Apache Commons IO
final byte[] buffer = new byte[bufferSize];
int n;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
} finally {
input.close();
try (InputStream inputStream = getInputStream()) {
return IOUtils.copyLarge(inputStream, output, new byte[bufferSize]);
}
return count;
}
}

0 comments on commit e1db69f

Please sign in to comment.