提交 abc9cec3 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved compatibility with the Java 7 FileSystem abstraction.

上级 5c412dbc
...@@ -1483,8 +1483,8 @@ As an example, to use the the <code>nio</code> file system, use the following da ...@@ -1483,8 +1483,8 @@ As an example, to use the the <code>nio</code> file system, use the following da
<code>jdbc:h2:nio:~/test</code>. <code>jdbc:h2:nio:~/test</code>.
</p> </p>
<p> <p>
To register a new file system, extend the classes <code>org.h2.store.fs.FileSystem, FileObject</code>, To register a new file system, extend the classes <code>org.h2.store.fs.FilePath, FileObject</code>,
and call the method <code>FileSystem.register</code> before using it. and call the method <code>FilePath.register</code> before using it.
</p> </p>
<p> <p>
For input streams (but not for random access files), URLs may be used in addition to the registered file systems. For input streams (but not for random access files), URLs may be used in addition to the registered file systems.
......
...@@ -1479,7 +1479,7 @@ public class Database implements DataHandler { ...@@ -1479,7 +1479,7 @@ public class Database implements DataHandler {
String path = FileUtils.getParent(databaseName); String path = FileUtils.getParent(databaseName);
String[] list = FileUtils.listFiles(path); String[] list = FileUtils.listFiles(path);
for (String name : list) { for (String name : list) {
if (name.endsWith(Constants.SUFFIX_TEMP_FILE) && FileUtils.fileStartsWith(name, databaseName)) { if (name.endsWith(Constants.SUFFIX_TEMP_FILE) && name.startsWith(databaseName)) {
// can't always delete the files, they may still be open // can't always delete the files, they may still be open
FileUtils.tryDelete(name); FileUtils.tryDelete(name);
} }
......
...@@ -73,13 +73,13 @@ public class FileLister { ...@@ -73,13 +73,13 @@ public class FileLister {
*/ */
public static ArrayList<String> getDatabaseFiles(String dir, String db, boolean all) { public static ArrayList<String> getDatabaseFiles(String dir, String db, boolean all) {
ArrayList<String> files = New.arrayList(); ArrayList<String> files = New.arrayList();
String start = db == null ? null : FileUtils.getCanonicalPath(dir + "/" + db); String start = db == null ? null : FileUtils.getCanonicalPath(dir + "/" + db + ".");
String[] list = FileUtils.listFiles(dir); String[] list = FileUtils.listFiles(dir);
for (int i = 0; list != null && i < list.length; i++) { for (int i = 0; list != null && i < list.length; i++) {
String f = list[i]; String f = list[i];
boolean ok = false; boolean ok = false;
if (f.endsWith(Constants.SUFFIX_LOBS_DIRECTORY)) { if (f.endsWith(Constants.SUFFIX_LOBS_DIRECTORY)) {
if (start == null || FileUtils.fileStartsWith(f, start + ".")) { if (start == null || f.startsWith(start)) {
files.addAll(getDatabaseFiles(f, null, all)); files.addAll(getDatabaseFiles(f, null, all));
ok = true; ok = true;
} }
...@@ -97,7 +97,7 @@ public class FileLister { ...@@ -97,7 +97,7 @@ public class FileLister {
} }
} }
if (ok) { if (ok) {
if (db == null || FileUtils.fileStartsWith(f, start + ".")) { if (db == null || f.startsWith(start)) {
String fileName = f; String fileName = f;
files.add(fileName); files.add(fileName);
} }
......
...@@ -34,6 +34,10 @@ public abstract class FilePath { ...@@ -34,6 +34,10 @@ public abstract class FilePath {
private static String tempRandom; private static String tempRandom;
private static long tempSequence; private static long tempSequence;
/**
* The complete path (which may be absolute or relative, depending on the
* file system).
*/
protected String name; protected String name;
/** /**
...@@ -199,14 +203,6 @@ public abstract class FilePath { ...@@ -199,14 +203,6 @@ public abstract class FilePath {
return idx < 0 ? name : name.substring(idx + 1); return idx < 0 ? name : name.substring(idx + 1);
} }
/**
* Check if a file starts with a given prefix.
*
* @param prefix the prefix
* @return true if it starts with the prefix
*/
public abstract boolean fileStartsWith(String prefix);
/** /**
* Create an output stream to write into the file. * Create an output stream to write into the file.
* *
...@@ -299,8 +295,8 @@ public abstract class FilePath { ...@@ -299,8 +295,8 @@ public abstract class FilePath {
* return an object even if the scheme doesn't match in case of the the * return an object even if the scheme doesn't match in case of the the
* default file provider. * default file provider.
* *
* @param path * @param path the path
* @return the file path * @return the file path object
*/ */
public abstract FilePath getPath(String path); public abstract FilePath getPath(String path);
......
...@@ -22,7 +22,6 @@ import org.h2.constant.SysProperties; ...@@ -22,7 +22,6 @@ import org.h2.constant.SysProperties;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.New; import org.h2.util.New;
import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
/** /**
...@@ -31,7 +30,6 @@ import org.h2.util.Utils; ...@@ -31,7 +30,6 @@ import org.h2.util.Utils;
*/ */
public class FilePathDisk extends FilePath { public class FilePathDisk extends FilePath {
private static final boolean IS_FILE_SYSTEM_CASE_INSENSITIVE = File.separatorChar == '\\';
private static final String CLASSPATH_PREFIX = "classpath:"; private static final String CLASSPATH_PREFIX = "classpath:";
public FilePathDisk getPath(String path) { public FilePathDisk getPath(String path) {
...@@ -246,16 +244,6 @@ public class FilePathDisk extends FilePath { ...@@ -246,16 +244,6 @@ public class FilePathDisk extends FilePath {
} }
} }
public boolean fileStartsWith(String prefix) {
prefix = translateFileName(prefix);
String fileName = name;
if (IS_FILE_SYSTEM_CASE_INSENSITIVE) {
fileName = StringUtils.toUpperEnglish(fileName);
prefix = StringUtils.toUpperEnglish(prefix);
}
return fileName.startsWith(prefix);
}
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) {
try { try {
File file = new File(name); File file = new File(name);
...@@ -338,14 +326,6 @@ public class FilePathDisk extends FilePath { ...@@ -338,14 +326,6 @@ public class FilePathDisk extends FilePath {
return f; return f;
} }
protected boolean accepts() {
return true;
}
public String unwrap(String fileName) {
return fileName;
}
public String getScheme() { public String getScheme() {
return "file"; return "file";
} }
......
...@@ -120,11 +120,6 @@ public class FilePathMem extends FilePath { ...@@ -120,11 +120,6 @@ public class FilePathMem extends FilePath {
// TODO directories are not really supported // TODO directories are not really supported
} }
public boolean fileStartsWith(String prefix) {
prefix = getCanonicalPath(prefix);
return name.startsWith(prefix);
}
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) {
try { try {
FileObjectMemData obj = getMemoryFile(); FileObjectMemData obj = getMemoryFile();
...@@ -161,14 +156,6 @@ public class FilePathMem extends FilePath { ...@@ -161,14 +156,6 @@ public class FilePathMem extends FilePath {
return name.equals(getScheme()); return name.equals(getScheme());
} }
protected boolean accepts(String fileName) {
return fileName.startsWith(getScheme());
}
public String unwrap(String fileName) {
return fileName;
}
private static String getCanonicalPath(String fileName) { private static String getCanonicalPath(String fileName) {
fileName = fileName.replace('\\', '/'); fileName = fileName.replace('\\', '/');
int idx = fileName.indexOf(':') + 1; int idx = fileName.indexOf(':') + 1;
...@@ -182,6 +169,11 @@ public class FilePathMem extends FilePath { ...@@ -182,6 +169,11 @@ public class FilePathMem extends FilePath {
return "memFS"; return "memFS";
} }
/**
* Whether the file should be compressed.
*
* @return if it should be compressed.
*/
boolean compressed() { boolean compressed() {
return false; return false;
} }
......
...@@ -14,23 +14,10 @@ import java.io.IOException; ...@@ -14,23 +14,10 @@ import java.io.IOException;
*/ */
public class FilePathNio extends FilePathWrapper { public class FilePathNio extends FilePathWrapper {
/** public FileObject openFileObject(String mode) throws IOException {
* Try to open a file with this name and mode. return new FileObjectNio(name, mode);
*
* @param fileName the file name
* @param mode the open mode
* @return the file object
* @throws IOException if opening fails
*/
protected FileObject open(String fileName, String mode) throws IOException {
return new FileObjectNio(fileName, mode);
} }
/**
* Get the prefix for this file system.
*
* @return the prefix
*/
public String getScheme() { public String getScheme() {
return "nio"; return "nio";
} }
......
...@@ -14,15 +14,10 @@ import java.io.IOException; ...@@ -14,15 +14,10 @@ import java.io.IOException;
*/ */
public class FilePathNioMapped extends FilePathNio { public class FilePathNioMapped extends FilePathNio {
protected FileObject open(String mode) throws IOException { public FileObject openFileObject(String mode) throws IOException {
return new FileObjectNioMapped(name, mode); return new FileObjectNioMapped(name, mode);
} }
/**
* Get the prefix for this file system.
*
* @return the prefix
*/
public String getScheme() { public String getScheme() {
return "nioMapped"; return "nioMapped";
} }
......
...@@ -24,6 +24,12 @@ public abstract class FilePathWrapper extends FilePath { ...@@ -24,6 +24,12 @@ public abstract class FilePathWrapper extends FilePath {
return create(path, unwrap(path)); return create(path, unwrap(path));
} }
/**
* Create a wrapped path instance for the given base path.
*
* @param base the base path
* @return the wrapped path
*/
public FilePathWrapper wrap(FilePath base) { public FilePathWrapper wrap(FilePath base) {
return base == null ? null : create(getPrefix() + base.name, base); return base == null ? null : create(getPrefix() + base.name, base);
} }
...@@ -47,6 +53,12 @@ public abstract class FilePathWrapper extends FilePath { ...@@ -47,6 +53,12 @@ public abstract class FilePathWrapper extends FilePath {
return getScheme() + ":"; return getScheme() + ":";
} }
/**
* Get the base path for the given wrapped path.
*
* @param path the path including the scheme prefix
* @return the base file path
*/
protected FilePath unwrap(String path) { protected FilePath unwrap(String path) {
return FilePath.get(path.substring(getScheme().length() + 1)); return FilePath.get(path.substring(getScheme().length() + 1));
} }
...@@ -75,10 +87,6 @@ public abstract class FilePathWrapper extends FilePath { ...@@ -75,10 +87,6 @@ public abstract class FilePathWrapper extends FilePath {
return base.exists(); return base.exists();
} }
public boolean fileStartsWith(String prefix) {
return name.startsWith(prefix);
}
public FilePath getParent() { public FilePath getParent() {
return wrap(base.getParent()); return wrap(base.getParent());
} }
......
...@@ -54,10 +54,6 @@ public class FilePathZip extends FilePath { ...@@ -54,10 +54,6 @@ public class FilePathZip extends FilePath {
} }
} }
public boolean fileStartsWith(String fileName, String prefix) {
return fileName.startsWith(prefix);
}
public long lastModified() { public long lastModified() {
return 0; return 0;
} }
...@@ -213,14 +209,6 @@ public class FilePathZip extends FilePath { ...@@ -213,14 +209,6 @@ public class FilePathZip extends FilePath {
return new FilePathDisk().getPath(name).createTempFile(suffix, deleteOnExit, true); return new FilePathDisk().getPath(name).createTempFile(suffix, deleteOnExit, true);
} }
// protected boolean accepts(String fileName) {
// return fileName.startsWith(PREFIX);
// }
public boolean fileStartsWith(String prefix) {
return name.startsWith(prefix);
}
public String getScheme() { public String getScheme() {
return "zip"; return "zip";
} }
......
...@@ -9,11 +9,11 @@ import org.h2.message.DbException; ...@@ -9,11 +9,11 @@ import org.h2.message.DbException;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
/** /**
* This utility class contains utility functions that use the file system abstraction. * This utility class contains utility functions that use the file system
* abstraction.
*/ */
public class FileUtils { public class FileUtils {
/** /**
* Checks if a file exists. * Checks if a file exists.
* This method is similar to Java 7 <code>java.nio.file.Path.exists</code>. * This method is similar to Java 7 <code>java.nio.file.Path.exists</code>.
...@@ -237,17 +237,6 @@ public class FileUtils { ...@@ -237,17 +237,6 @@ public class FileUtils {
return FilePath.get(fileName).unwrap().toString(); return FilePath.get(fileName).unwrap().toString();
} }
/**
* Check if a file starts with a given prefix.
*
* @param fileName the complete file name
* @param prefix the prefix
* @return true if it starts with the prefix
*/
public static boolean fileStartsWith(String fileName, String prefix) {
return FilePath.get(fileName).fileStartsWith(prefix);
}
// utility methods ======================================= // utility methods =======================================
/** /**
......
...@@ -76,11 +76,6 @@ public class FilePathDebug extends FilePathWrapper { ...@@ -76,11 +76,6 @@ public class FilePathDebug extends FilePathWrapper {
return super.exists(); return super.exists();
} }
public boolean fileStartsWith(String prefix) {
trace(name, "fileStartsWith", unwrap(prefix));
return super.fileStartsWith(prefix);
}
public String getName() { public String getName() {
trace(name, "getName"); trace(name, "getName");
return super.getName(); return super.getName();
......
...@@ -692,5 +692,5 @@ unusual apfel caught overcareful tricky nodep junit eventually concrete ...@@ -692,5 +692,5 @@ unusual apfel caught overcareful tricky nodep junit eventually concrete
enhancer banana nit cglib challenging intercepted banane assertthrows enhancer banana nit cglib challenging intercepted banane assertthrows
objenesis prepend detecting overridable eater forgetting tear objenesis prepend detecting overridable eater forgetting tear
fork tester jaspa redirection johnny brings gone jooq iciql offline pdo mappings largely fork tester jaspa redirection johnny brings gone jooq iciql offline pdo mappings largely
pst patadia summertime jalpesh pst patadia summertime jalpesh scheme compilable
...@@ -22,6 +22,9 @@ import org.h2.store.fs.FileUtils; ...@@ -22,6 +22,9 @@ import org.h2.store.fs.FileUtils;
*/ */
public class FilePathCrypt extends FilePathWrapper { public class FilePathCrypt extends FilePathWrapper {
/**
* Register this file system.
*/
public static void register() { public static void register() {
FilePath.register(new FilePathCrypt()); FilePath.register(new FilePathCrypt());
} }
......
...@@ -297,10 +297,6 @@ public class FilePathZip2 extends FilePath { ...@@ -297,10 +297,6 @@ public class FilePathZip2 extends FilePath {
return FilePathDisk.expandUserHomeDirectory(fileName); return FilePathDisk.expandUserHomeDirectory(fileName);
} }
public boolean fileStartsWith(String prefix) {
return name.startsWith(prefix);
}
public String getScheme() { public String getScheme() {
return "zip2"; return "zip2";
} }
......
...@@ -88,7 +88,6 @@ public class FtpData extends Thread { ...@@ -88,7 +88,6 @@ public class FtpData extends Thread {
/** /**
* Read a file from a client. * Read a file from a client.
* *
* @param fs the target file system
* @param fileName the target file name * @param fileName the target file name
*/ */
synchronized void receive(String fileName) throws IOException { synchronized void receive(String fileName) throws IOException {
...@@ -108,7 +107,6 @@ public class FtpData extends Thread { ...@@ -108,7 +107,6 @@ public class FtpData extends Thread {
* Send a file to the client. This method waits until the client has * Send a file to the client. This method waits until the client has
* connected. * connected.
* *
* @param fs the source file system
* @param fileName the source file name * @param fileName the source file name
* @param skip the number of bytes to skip * @param skip the number of bytes to skip
*/ */
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论