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