use of org.h2.store.fs.FileChannelOutputStream in project h2database by h2database.
the class ChangeFileEncryption method copy.
private void copy(String fileName, boolean quiet) throws IOException {
if (FileUtils.isDirectory(fileName)) {
return;
}
String temp = directory + "/temp.db";
try (FileChannel fileIn = getFileChannel(fileName, "r", decryptKey)) {
try (InputStream inStream = new FileChannelInputStream(fileIn, true)) {
FileUtils.delete(temp);
try (OutputStream outStream = new FileChannelOutputStream(getFileChannel(temp, "rw", encryptKey), true)) {
byte[] buffer = new byte[4 * 1024];
long remaining = fileIn.size();
long total = remaining;
long time = System.nanoTime();
while (remaining > 0) {
if (!quiet && System.nanoTime() - time > TimeUnit.SECONDS.toNanos(1)) {
out.println(fileName + ": " + (100 - 100 * remaining / total) + "%");
time = System.nanoTime();
}
int len = (int) Math.min(buffer.length, remaining);
len = inStream.read(buffer, 0, len);
outStream.write(buffer, 0, len);
remaining -= len;
}
}
}
}
FileUtils.delete(fileName);
FileUtils.move(temp, fileName);
}
Aggregations