Search in sources :

Example 1 with FileChannelInputStream

use of org.h2.store.fs.FileChannelInputStream 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)

Example 2 with FileChannelInputStream

use of org.h2.store.fs.FileChannelInputStream in project h2database by h2database.

the class TestConcurrent method copyFileSlowly.

private static void copyFileSlowly(FileChannel file, long length, OutputStream out) throws Exception {
    file.position(0);
    InputStream in = new BufferedInputStream(new FileChannelInputStream(file, false));
    for (int j = 0; j < length; j++) {
        int x = in.read();
        if (x < 0) {
            break;
        }
        out.write(x);
    }
    in.close();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileChannelInputStream(org.h2.store.fs.FileChannelInputStream) BufferedInputStream(java.io.BufferedInputStream) FileChannelInputStream(org.h2.store.fs.FileChannelInputStream) InputStream(java.io.InputStream)

Aggregations

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