Search in sources :

Example 1 with FileChannelOutputStream

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);
}
Also used : FileChannelInputStream(org.h2.store.fs.FileChannelInputStream) FileChannel(java.nio.channels.FileChannel) FileChannelInputStream(org.h2.store.fs.FileChannelInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileChannelOutputStream(org.h2.store.fs.FileChannelOutputStream) FileChannelOutputStream(org.h2.store.fs.FileChannelOutputStream)

Aggregations

InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 FileChannel (java.nio.channels.FileChannel)1 FileChannelInputStream (org.h2.store.fs.FileChannelInputStream)1 FileChannelOutputStream (org.h2.store.fs.FileChannelOutputStream)1