Search in sources :

Example 1 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class TestFile method doTest.

private void doTest(boolean nioMem, boolean compress) {
    int len = getSize(1000, 10000);
    Random random = new Random();
    FileStore mem = null, file = null;
    byte[] buffMem = null;
    byte[] buffFile = null;
    String prefix = nioMem ? (compress ? "nioMemLZF:" : "nioMemFS:") : (compress ? "memLZF:" : "memFS:");
    FileUtils.delete(prefix + "test");
    FileUtils.delete("~/testFile");
    for (int i = 0; i < len; i++) {
        if (buffMem == null) {
            int l = 1 + random.nextInt(1000);
            buffMem = new byte[l];
            buffFile = new byte[l];
        }
        if (file == null) {
            mem = FileStore.open(this, prefix + "test", "rw");
            file = FileStore.open(this, "~/testFile", "rw");
        }
        assertEquals(file.getFilePointer(), mem.getFilePointer());
        assertEquals(file.length(), mem.length());
        int x = random.nextInt(100);
        if ((x -= 20) < 0) {
            if (file.length() > 0) {
                long pos = random.nextInt((int) (file.length() / 16)) * 16;
                trace("seek " + pos);
                mem.seek(pos);
                file.seek(pos);
            }
        } else if ((x -= 20) < 0) {
            trace("close");
            mem.close();
            file.close();
            mem = null;
            file = null;
        } else if ((x -= 20) < 0) {
            if (buffFile.length > 16) {
                random.nextBytes(buffFile);
                System.arraycopy(buffFile, 0, buffMem, 0, buffFile.length);
                int off = random.nextInt(buffFile.length - 16);
                int l = random.nextInt((buffFile.length - off) / 16) * 16;
                trace("write " + off + " " + l);
                mem.write(buffMem, off, l);
                file.write(buffFile, off, l);
            }
        } else if ((x -= 20) < 0) {
            if (buffFile.length > 16) {
                int off = random.nextInt(buffFile.length - 16);
                int l = random.nextInt((buffFile.length - off) / 16) * 16;
                l = (int) Math.min(l, file.length() - file.getFilePointer());
                trace("read " + off + " " + l);
                Exception a = null, b = null;
                try {
                    file.readFully(buffFile, off, l);
                } catch (Exception e) {
                    a = e;
                }
                try {
                    mem.readFully(buffMem, off, l);
                } catch (Exception e) {
                    b = e;
                }
                if (a != b) {
                    if (a == null || b == null) {
                        fail("only one threw an exception");
                    }
                }
                assertEquals(buffMem, buffFile);
            }
        } else if ((x -= 10) < 0) {
            trace("reset buffers");
            buffMem = null;
            buffFile = null;
        } else {
            int l = random.nextInt(10000) * 16;
            long p = file.getFilePointer();
            file.setLength(l);
            mem.setLength(l);
            trace("setLength " + l);
            if (p > l) {
                file.seek(l);
                mem.seek(l);
            }
        }
    }
    if (mem != null) {
        mem.close();
        file.close();
    }
    FileUtils.delete(prefix + "test");
    FileUtils.delete("~/testFile");
}
Also used : FileStore(org.h2.store.FileStore) Random(java.util.Random)

Example 2 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class CreateScriptFile method openScriptReader.

/**
 * Open a script reader.
 *
 * @param fileName the file name (the file will be overwritten)
 * @param compressionAlgorithm the compression algorithm (uppercase)
 * @param cipher the encryption algorithm or null
 * @param password the encryption password
 * @param charset the character set (for example UTF-8)
 * @return the script reader
 */
public static LineNumberReader openScriptReader(String fileName, String compressionAlgorithm, String cipher, String password, String charset) throws IOException {
    try {
        InputStream in;
        if (cipher != null) {
            byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray());
            FileStore store = FileStore.open(null, fileName, "rw", cipher, key);
            store.init();
            in = new FileStoreInputStream(store, null, compressionAlgorithm != null, false);
            in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE_COMPRESS);
        } else {
            in = FileUtils.newInputStream(fileName);
            in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE);
            in = CompressTool.wrapInputStream(in, compressionAlgorithm, "script.sql");
            if (in == null) {
                throw new IOException("Entry not found: script.sql in " + fileName);
            }
        }
        return new LineNumberReader(new InputStreamReader(in, charset));
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : FileStore(org.h2.store.FileStore) InputStreamReader(java.io.InputStreamReader) FileStoreInputStream(org.h2.store.FileStoreInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 3 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class ValueLob method getInputStream.

@Override
public InputStream getInputStream() {
    if (fileName == null) {
        return new ByteArrayInputStream(small);
    }
    FileStore store = handler.openFile(fileName, "r", true);
    boolean alwaysClose = SysProperties.lobCloseBetweenReads;
    return new BufferedInputStream(new FileStoreInputStream(store, handler, compressed, alwaysClose), Constants.IO_BUFFER_SIZE);
}
Also used : FileStore(org.h2.store.FileStore) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream)

Example 4 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class ChangeFileEncryption method copy.

private void copy(String fileName, FileStore in, byte[] key, boolean quiet) {
    if (FileUtils.isDirectory(fileName)) {
        return;
    }
    String temp = directory + "/temp.db";
    FileUtils.delete(temp);
    FileStore fileOut;
    if (key == null) {
        fileOut = FileStore.open(null, temp, "rw");
    } else {
        fileOut = FileStore.open(null, temp, "rw", cipherType, key);
    }
    fileOut.init();
    byte[] buffer = new byte[4 * 1024];
    long remaining = in.length() - FileStore.HEADER_LENGTH;
    long total = remaining;
    in.seek(FileStore.HEADER_LENGTH);
    fileOut.seek(FileStore.HEADER_LENGTH);
    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);
        in.readFully(buffer, 0, len);
        fileOut.write(buffer, 0, len);
        remaining -= len;
    }
    in.close();
    fileOut.close();
    FileUtils.delete(fileName);
    FileUtils.move(temp, fileName);
}
Also used : FileStore(org.h2.store.FileStore)

Example 5 with FileStore

use of org.h2.store.FileStore in project h2database by h2database.

the class Recover method dumpLob.

private void dumpLob(String fileName, boolean lobCompression) {
    OutputStream fileOut = null;
    FileStore fileStore = null;
    long size = 0;
    String n = fileName + (lobCompression ? ".comp" : "") + ".txt";
    InputStream in = null;
    try {
        fileOut = FileUtils.newOutputStream(n, false);
        fileStore = FileStore.open(null, fileName, "r");
        fileStore.init();
        in = new FileStoreInputStream(fileStore, this, lobCompression, false);
        size = IOUtils.copy(in, fileOut);
    } catch (Throwable e) {
    // this is usually not a problem, because we try both compressed and
    // uncompressed
    } finally {
        IOUtils.closeSilently(fileOut);
        IOUtils.closeSilently(in);
        closeSilently(fileStore);
    }
    if (size == 0) {
        try {
            FileUtils.delete(n);
        } catch (Exception e) {
            traceError(n, e);
        }
    }
}
Also used : FileStore(org.h2.store.FileStore) FileStoreInputStream(org.h2.store.FileStoreInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileStoreInputStream(org.h2.store.FileStoreInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

FileStore (org.h2.store.FileStore)10 IOException (java.io.IOException)6 BufferedInputStream (java.io.BufferedInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileStoreInputStream (org.h2.store.FileStoreInputStream)5 InputStream (java.io.InputStream)4 MVStore (org.h2.mvstore.MVStore)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 DbException (org.h2.message.DbException)3 FileStore (org.h2.mvstore.FileStore)3 InputStreamReader (java.io.InputStreamReader)2 OutputStream (java.io.OutputStream)2 OffHeapStore (org.h2.mvstore.OffHeapStore)2 BufferedOutputStream (java.io.BufferedOutputStream)1 LineNumberReader (java.io.LineNumberReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 SequenceInputStream (java.io.SequenceInputStream)1 ByteBuffer (java.nio.ByteBuffer)1