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-vfs.git

commit e1db69fa21494e35bc594098c75c70f2829da8f5
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sat Mar 1 10:23:33 2025 -0500

    Reuse IOUtils in write(OutputStream, int)
---
 .../commons/vfs2/provider/DefaultFileContent.java    | 20 +++++---------------
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
index d8af8f052..7c0c24c18 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java
@@ -25,6 +25,7 @@ import java.util.Collections;
 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;
@@ -848,29 +849,18 @@ public final class DefaultFileContent implements 
FileContent {
     }
 
     /**
-     * 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;
     }
 }

Reply via email to