use of org.h2.store.fs.FilePath in project h2database by h2database.
the class TestFileSystem method testZipFileSystem.
private void testZipFileSystem(String prefix, Random r) throws IOException {
byte[] data = new byte[r.nextInt(16 * 1024)];
long x = r.nextLong();
FilePath file = FilePath.get(getBaseDir() + "/fs/readonly" + x + ".zip");
r.nextBytes(data);
OutputStream out = file.newOutputStream(false);
ZipOutputStream zipOut = new ZipOutputStream(out);
ZipEntry entry = new ZipEntry("data");
zipOut.putNextEntry(entry);
zipOut.write(data);
zipOut.closeEntry();
zipOut.close();
out.close();
FilePath fp = FilePath.get(prefix + getBaseDir() + "/fs/readonly" + x + ".zip!data");
FileChannel fc = fp.open("r");
StringBuilder buff = new StringBuilder();
try {
int pos = 0;
for (int i = 0; i < 100; i++) {
trace("op " + i);
switch(r.nextInt(5)) {
case 0:
{
int p = r.nextInt(data.length);
trace("seek " + p);
buff.append("seek " + p + "\n");
fc.position(p);
pos = p;
break;
}
case 1:
{
int len = r.nextInt(1000);
int offset = r.nextInt(100);
int arrayLen = len + offset;
len = Math.min(len, data.length - pos);
byte[] b1 = new byte[arrayLen];
byte[] b2 = new byte[arrayLen];
trace("readFully " + len);
buff.append("readFully " + len + "\n");
System.arraycopy(data, pos, b1, offset, len);
ByteBuffer byteBuff = createSlicedBuffer(b2, offset, len);
FileUtils.readFully(fc, byteBuff);
assertEquals(b1, b2);
pos += len;
break;
}
case 2:
{
int len = r.nextInt(1000);
int offset = r.nextInt(100);
int arrayLen = len + offset;
int p = r.nextInt(data.length);
len = Math.min(len, data.length - p);
byte[] b1 = new byte[arrayLen];
byte[] b2 = new byte[arrayLen];
trace("readFully " + p + " " + len);
buff.append("readFully " + p + " " + len + "\n");
System.arraycopy(data, p, b1, offset, len);
ByteBuffer byteBuff = createSlicedBuffer(b2, offset, len);
DataUtils.readFully(fc, p, byteBuff);
assertEquals(b1, b2);
break;
}
case 3:
{
trace("getFilePointer");
buff.append("getFilePointer\n");
assertEquals(pos, fc.position());
break;
}
case 4:
{
trace("length " + data.length);
buff.append("length " + data.length + "\n");
assertEquals(data.length, fc.size());
break;
}
default:
}
}
fc.close();
file.delete();
} catch (Throwable e) {
e.printStackTrace();
fail("Exception: " + e + "\n" + buff.toString());
}
}
use of org.h2.store.fs.FilePath in project h2database by h2database.
the class FileStore method open.
/**
* Try to open the file.
*
* @param fileName the file name
* @param readOnly whether the file should only be opened in read-only mode,
* even if the file is writable
* @param encryptionKey the encryption key, or null if encryption is not
* used
*/
public void open(String fileName, boolean readOnly, char[] encryptionKey) {
if (file != null) {
return;
}
if (fileName != null) {
// ensure the Cache file system is registered
FilePathCache.INSTANCE.getScheme();
FilePath p = FilePath.get(fileName);
// if no explicit scheme was specified, NIO is used
if (p instanceof FilePathDisk && !fileName.startsWith(p.getScheme() + ":")) {
// ensure the NIO file system is registered
FilePathNio.class.getName();
fileName = "nio:" + fileName;
}
}
this.fileName = fileName;
FilePath f = FilePath.get(fileName);
FilePath parent = f.getParent();
if (parent != null && !parent.exists()) {
throw DataUtils.newIllegalArgumentException("Directory does not exist: {0}", parent);
}
if (f.exists() && !f.canWrite()) {
readOnly = true;
}
this.readOnly = readOnly;
try {
file = f.open(readOnly ? "r" : "rw");
if (encryptionKey != null) {
byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey);
encryptedFile = file;
file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
}
try {
if (readOnly) {
fileLock = file.tryLock(0, Long.MAX_VALUE, true);
} else {
fileLock = file.tryLock();
}
} catch (OverlappingFileLockException e) {
throw DataUtils.newIllegalStateException(DataUtils.ERROR_FILE_LOCKED, "The file is locked: {0}", fileName, e);
}
if (fileLock == null) {
throw DataUtils.newIllegalStateException(DataUtils.ERROR_FILE_LOCKED, "The file is locked: {0}", fileName);
}
fileSize = file.size();
} catch (IOException e) {
throw DataUtils.newIllegalStateException(DataUtils.ERROR_READING_FAILED, "Could not open file {0}", fileName, e);
}
}
use of org.h2.store.fs.FilePath in project h2database by h2database.
the class TestFileSystem method testParentEventuallyReturnsNull.
private void testParentEventuallyReturnsNull(String fsBase) {
FilePath p = FilePath.get(fsBase + "/testFile");
assertTrue(p.getScheme().length() > 0);
for (int i = 0; i < 100; i++) {
if (p == null) {
return;
}
p = p.getParent();
}
fail("Parent is not null: " + p);
String path = fsBase + "/testFile";
for (int i = 0; i < 100; i++) {
if (path == null) {
return;
}
path = FileUtils.getParent(path);
}
fail("Parent is not null: " + path);
}
use of org.h2.store.fs.FilePath in project h2database by h2database.
the class TestFileSystem method testSimple.
private void testSimple(final String fsBase) throws Exception {
long time = System.currentTimeMillis();
for (String s : FileUtils.newDirectoryStream(fsBase)) {
FileUtils.delete(s);
}
FileUtils.createDirectories(fsBase + "/test");
assertTrue(FileUtils.exists(fsBase));
FileUtils.delete(fsBase + "/test");
FileUtils.delete(fsBase + "/test2");
assertTrue(FileUtils.createFile(fsBase + "/test"));
List<FilePath> p = FilePath.get(fsBase).newDirectoryStream();
assertEquals(1, p.size());
String can = FilePath.get(fsBase + "/test").toRealPath().toString();
assertEquals(can, p.get(0).toString());
assertTrue(FileUtils.canWrite(fsBase + "/test"));
FileChannel channel = FileUtils.open(fsBase + "/test", "rw");
byte[] buffer = new byte[10000];
Random random = new Random(1);
random.nextBytes(buffer);
channel.write(ByteBuffer.wrap(buffer));
assertEquals(10000, channel.size());
channel.position(20000);
assertEquals(20000, channel.position());
assertEquals(-1, channel.read(ByteBuffer.wrap(buffer, 0, 1)));
String path = fsBase + "/test";
assertEquals("test", FileUtils.getName(path));
can = FilePath.get(fsBase).toRealPath().toString();
String can2 = FileUtils.toRealPath(FileUtils.getParent(path));
assertEquals(can, can2);
FileLock lock = channel.tryLock();
if (lock != null) {
lock.release();
}
assertEquals(10000, channel.size());
channel.close();
assertEquals(10000, FileUtils.size(fsBase + "/test"));
channel = FileUtils.open(fsBase + "/test", "r");
final byte[] test = new byte[10000];
FileUtils.readFully(channel, ByteBuffer.wrap(test, 0, 10000));
assertEquals(buffer, test);
final FileChannel fc = channel;
new AssertThrows(IOException.class) {
@Override
public void test() throws Exception {
fc.write(ByteBuffer.wrap(test, 0, 10));
}
};
new AssertThrows(NonWritableChannelException.class) {
@Override
public void test() throws Exception {
fc.truncate(10);
}
};
channel.close();
long lastMod = FileUtils.lastModified(fsBase + "/test");
if (lastMod < time - 1999) {
// at most 2 seconds difference
assertEquals(time, lastMod);
}
assertEquals(10000, FileUtils.size(fsBase + "/test"));
List<String> list = FileUtils.newDirectoryStream(fsBase);
assertEquals(1, list.size());
assertTrue(list.get(0).endsWith("test"));
IOUtils.copyFiles(fsBase + "/test", fsBase + "/test3");
FileUtils.move(fsBase + "/test3", fsBase + "/test2");
FileUtils.move(fsBase + "/test2", fsBase + "/test2");
assertFalse(FileUtils.exists(fsBase + "/test3"));
assertTrue(FileUtils.exists(fsBase + "/test2"));
assertEquals(10000, FileUtils.size(fsBase + "/test2"));
byte[] buffer2 = new byte[10000];
InputStream in = FileUtils.newInputStream(fsBase + "/test2");
int pos = 0;
while (true) {
int l = in.read(buffer2, pos, Math.min(10000 - pos, 1000));
if (l <= 0) {
break;
}
pos += l;
}
in.close();
assertEquals(10000, pos);
assertEquals(buffer, buffer2);
assertTrue(FileUtils.tryDelete(fsBase + "/test2"));
FileUtils.delete(fsBase + "/test");
if (fsBase.indexOf("memFS:") < 0 && fsBase.indexOf("memLZF:") < 0 && fsBase.indexOf("nioMemFS:") < 0 && fsBase.indexOf("nioMemLZF:") < 0) {
FileUtils.createDirectories(fsBase + "/testDir");
assertTrue(FileUtils.isDirectory(fsBase + "/testDir"));
if (!fsBase.startsWith("jdbc:")) {
FileUtils.deleteRecursive(fsBase + "/testDir", false);
assertFalse(FileUtils.exists(fsBase + "/testDir"));
}
}
}
use of org.h2.store.fs.FilePath in project h2database by h2database.
the class TestFileSystem method testRootExists.
private void testRootExists(String fsBase) {
String fileName = fsBase + "/testFile";
FilePath p = FilePath.get(fileName);
while (p.getParent() != null) {
p = p.getParent();
}
assertTrue(p.exists());
}
Aggregations