Search in sources :

Example 16 with FileStore

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

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
 */
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, null, 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)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