Skip to content

Commit

Permalink
Rewrite copyFile using Files.copy.
Browse files Browse the repository at this point in the history
We had some tests observe incomplete copies pretty frequently, and this
seems to fix it. If for some reason this turns out to be noticeably less
efficient than FileChannel, we can rewrite it back to FileChannel and
loop to make sure it copies everything.

(Committed on behalf of a Googler.)
  • Loading branch information
juangj committed Mar 17, 2016
1 parent 09ec0b8 commit c10e8a9
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions java/client/src/org/openqa/selenium/io/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.List;

/**
Expand Down Expand Up @@ -185,20 +187,12 @@ private static void copyFile(File from, File to, Filter onlyCopy) throws IOExcep
return;
}

FileChannel out = null;
FileChannel in = null;
try {
in = new FileInputStream(from).getChannel();
out = new FileOutputStream(to).getChannel();
final long length = in.size();

final long copied = in.transferTo(0, in.size(), out);
try (OutputStream out = new FileOutputStream(to)) {
final long copied = Files.copy(from.toPath(), out);
final long length = from.length();
if (copied != length) {
throw new IOException("Could not transfer all bytes.");
}
} finally {
Closeables.close(out, false);
Closeables.close(in, false);
}
}

Expand Down

0 comments on commit c10e8a9

Please sign in to comment.