Search in sources :

Example 11 with FileStore

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

the class LobDataFile method getInputStream.

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

Example 12 with FileStore

use of org.h2.mvstore.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 13 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class SessionRemote method openFile.

@Override
public FileStore openFile(String name, String mode, boolean mustExist) {
    if (mustExist && !FileUtils.exists(name)) {
        throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name);
    }
    FileStore store;
    if (cipher == null) {
        store = FileStore.open(this, name, mode);
    } else {
        store = FileStore.open(this, name, mode, cipher, fileEncryptionKey, 0);
    }
    store.setCheckedWriting(false);
    try {
        store.init();
    } catch (DbException e) {
        store.closeSilently();
        throw e;
    }
    return store;
}
Also used : FileStore(org.h2.store.FileStore) DbException(org.h2.message.DbException)

Example 14 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class FilePathDisk method setReadOnly.

@Override
public boolean setReadOnly() {
    Path f = Paths.get(name);
    try {
        FileStore fileStore = Files.getFileStore(f);
        /*
             * Need to check PosixFileAttributeView first because
             * DosFileAttributeView is also supported by recent Java versions on
             * non-Windows file systems, but it doesn't affect real access
             * permissions.
             */
        if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
            HashSet<PosixFilePermission> permissions = new HashSet<>();
            for (PosixFilePermission p : Files.getPosixFilePermissions(f)) {
                switch(p) {
                    case OWNER_WRITE:
                    case GROUP_WRITE:
                    case OTHERS_WRITE:
                        break;
                    default:
                        permissions.add(p);
                }
            }
            Files.setPosixFilePermissions(f, permissions);
        } else if (fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
            Files.setAttribute(f, "dos:readonly", true);
        } else {
            return false;
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}
Also used : Path(java.nio.file.Path) FilePath(org.h2.store.fs.FilePath) FileStore(java.nio.file.FileStore) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 15 with FileStore

use of org.h2.mvstore.FileStore in project SpringStudy by myounghaklee.

the class CreateScriptFile method openScriptWriter.

/**
 * Open a script writer.
 *
 * @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 print writer
 * @throws IOException on failure
 */
public static PrintWriter openScriptWriter(String fileName, String compressionAlgorithm, String cipher, String password, String charset) throws IOException {
    try {
        OutputStream out;
        if (cipher != null) {
            byte[] key = SHA256.getKeyPasswordHash("script", password.toCharArray());
            FileUtils.delete(fileName);
            FileStore store = FileStore.open(null, fileName, "rw", cipher, key);
            store.init();
            out = new FileStoreOutputStream(store, compressionAlgorithm);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE_COMPRESS);
        } else {
            out = FileUtils.newOutputStream(fileName, false);
            out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE);
            out = CompressTool.wrapOutputStream(out, compressionAlgorithm, "script.sql");
        }
        return new PrintWriter(new OutputStreamWriter(out, charset));
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : FileStore(org.h2.store.FileStore) OutputStream(java.io.OutputStream) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) FileStoreOutputStream(org.h2.store.FileStoreOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

FileStore (org.h2.store.FileStore)20 FileStore (org.h2.mvstore.FileStore)16 IOException (java.io.IOException)14 MVStore (org.h2.mvstore.MVStore)11 BufferedInputStream (java.io.BufferedInputStream)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 FileStoreInputStream (org.h2.store.FileStoreInputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 BitSet (java.util.BitSet)6 HashSet (java.util.HashSet)6 DbException (org.h2.message.DbException)6 FileStoreOutputStream (org.h2.store.FileStoreOutputStream)6 InputStream (java.io.InputStream)5 InputStreamReader (java.io.InputStreamReader)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Setting (org.h2.engine.Setting)5 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OutputStream (java.io.OutputStream)3