Skip to content

Commit e1db69f

Browse files
committed
Reuse IOUtils in write(OutputStream, int)
1 parent 69531f0 commit e1db69f

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/DefaultFileContent.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Set;
2727

28+
import org.apache.commons.io.IOUtils;
2829
import org.apache.commons.lang3.ArrayUtils;
2930
import org.apache.commons.vfs2.FileContent;
3031
import org.apache.commons.vfs2.FileContentInfo;
@@ -848,29 +849,18 @@ public long write(final OutputStream output) throws IOException {
848849
}
849850

850851
/**
851-
* Writes this content to an OutputStream.
852+
* Copies this content to an OutputStream.
852853
*
853854
* @param output The target OutputStream.
854855
* @param bufferSize The buffer size to write data chunks.
855-
* @return the total number of bytes written
856+
* @return the total number of bytes written.
856857
* @throws IOException if an error occurs writing the file.
857858
* @since 2.1
858859
*/
859860
@Override
860861
public long write(final OutputStream output, final int bufferSize) throws IOException {
861-
final InputStream input = getInputStream();
862-
long count = 0;
863-
try {
864-
// This read/write code from Apache Commons IO
865-
final byte[] buffer = new byte[bufferSize];
866-
int n;
867-
while (-1 != (n = input.read(buffer))) {
868-
output.write(buffer, 0, n);
869-
count += n;
870-
}
871-
} finally {
872-
input.close();
862+
try (InputStream inputStream = getInputStream()) {
863+
return IOUtils.copyLarge(inputStream, output, new byte[bufferSize]);
873864
}
874-
return count;
875865
}
876866
}

0 commit comments

Comments
 (0)