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);
}
}
Aggregations