[libbluray-devel] Add support for relative paths to File.exists()

hpi1 git at videolan.org
Mon Jan 20 14:39:41 CET 2014


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Mon Jan 20 15:04:19 2014 +0200| [11c40410cb4dbf0505dff826e748e2b7c13ea4eb] | committer: hpi1

Add support for relative paths to File.exists()

(files inside Xlet-specific home directory).

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=11c40410cb4dbf0505dff826e748e2b7c13ea4eb
---

 .../bdj/java-j2me/java/io/BDFileSystemImpl.java    |   36 ++++
 .../bdj/java-j2se/java/io/BDFileSystemImpl.java    |   41 +++++
 src/libbluray/bdj/java/java/io/BDFileSystem.java   |  190 ++++++++++++++++++++
 .../bdj/java/org/videolan/BDJClassLoader.java      |   12 ++
 4 files changed, 279 insertions(+)

diff --git a/src/libbluray/bdj/java-j2me/java/io/BDFileSystemImpl.java b/src/libbluray/bdj/java-j2me/java/io/BDFileSystemImpl.java
new file mode 100644
index 0000000..e0b19d2
--- /dev/null
+++ b/src/libbluray/bdj/java-j2me/java/io/BDFileSystemImpl.java
@@ -0,0 +1,36 @@
+/*
+ * This file is part of libbluray
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package java.io;
+
+class BDFileSystemImpl extends BDFileSystem {
+
+    public BDFileSystemImpl(FileSystem fs) {
+        super(fs);
+    }
+
+    /* Different in SE */
+    public boolean checkAccess(File f, boolean write) {
+        return fs.checkAccess(f, write);
+    }
+
+    /* Not in SE */
+    public boolean deleteOnExit(File f) {
+        return fs.deleteOnExit(f);
+    }
+}
diff --git a/src/libbluray/bdj/java-j2se/java/io/BDFileSystemImpl.java b/src/libbluray/bdj/java-j2se/java/io/BDFileSystemImpl.java
new file mode 100644
index 0000000..2ea362f
--- /dev/null
+++ b/src/libbluray/bdj/java-j2se/java/io/BDFileSystemImpl.java
@@ -0,0 +1,41 @@
+/*
+ * This file is part of libbluray
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package java.io;
+
+class BDFileSystemImpl extends BDFileSystem {
+
+    public BDFileSystemImpl(FileSystem fs) {
+        super(fs);
+    }
+
+    /* different in ME */
+    public boolean checkAccess(File f, int access) {
+        return fs.checkAccess(f, access);
+    }
+
+    /* Not in ME */
+    public boolean setPermission(File f, int access, boolean enable, boolean owneronly) {
+        return fs.setPermission(f, access, enable, owneronly);
+    }
+
+    /* Not in ME */
+    public long getSpace(File f, int t) {
+        return fs.getSpace(f, t);
+    }
+}
diff --git a/src/libbluray/bdj/java/java/io/BDFileSystem.java b/src/libbluray/bdj/java/java/io/BDFileSystem.java
new file mode 100644
index 0000000..2cac57f
--- /dev/null
+++ b/src/libbluray/bdj/java/java/io/BDFileSystem.java
@@ -0,0 +1,190 @@
+/*
+ * This file is part of libbluray
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Wrapper for java.io.FileSystem class.
+ *
+ * - replace getBooleanAttributes() for relative paths.
+ *   Pretend files exist, if those are in xlet home directory (inside .jar).
+ *   No other relative paths are allowed.
+ */
+
+package java.io;
+
+import java.lang.reflect.Field;
+import java.net.URL;
+
+import org.videolan.BDJXletContext;
+
+public abstract class BDFileSystem extends FileSystem {
+
+    protected final FileSystem fs;
+
+    public static void init(Class c) {
+        Field filesystem;
+        try {
+            filesystem = c.getDeclaredField("fs");
+            filesystem.setAccessible(true);
+
+            FileSystem fs = (FileSystem)filesystem.get(null);
+            if (fs instanceof BDFileSystemImpl) {
+                //System.err.print("FileSystem already wrapped");
+            } else {
+                filesystem.set(null, new BDFileSystemImpl(fs));
+            }
+        } catch (Throwable t) {
+            System.err.print("Hooking FileSystem class failed: " + t);
+        }
+    }
+
+    public BDFileSystem(FileSystem fs) {
+        this.fs = fs;
+    }
+
+    public char getSeparator() {
+        return fs.getSeparator();
+    }
+
+    public char getPathSeparator() {
+        return fs.getPathSeparator();
+    }
+
+    public String normalize(String pathname) {
+        return fs.normalize(pathname);
+    }
+
+    public int prefixLength(String pathname) {
+        return fs.prefixLength(pathname);
+    }
+
+    public String resolve(String parent, String child) {
+        return fs.resolve(parent, child);
+    }
+
+    public String getDefaultParent() {
+        return fs.getDefaultParent();
+    }
+
+    public String fromURIPath(String path) {
+        return fs.fromURIPath(path);
+    }
+
+    public boolean isAbsolute(File f) {
+        return fs.isAbsolute(f);
+    }
+
+    public String resolve(File f) {
+        if (!f.isAbsolute()) {
+            System.err.println("***** resolve " + f);
+        }
+        return fs.resolve(f);
+    }
+
+    public String canonicalize(String path) throws IOException {
+        return fs.canonicalize(path);
+    }
+
+    public int getBooleanAttributes(File f) {
+        if (f.isAbsolute()) {
+            return fs.getBooleanAttributes(f);
+        }
+
+        /* try to locate file in Xlet home directory (inside JAR file) */
+        URL url = BDJXletContext.getCurrentResource(f.getPath());
+        if (url == null) {
+            return 0;
+        }
+
+        org.videolan.Logger.getLogger("BDFileSystem").info("Relative path " + f.getPath() + " translated to " + url);
+
+        return FileSystem.BA_EXISTS; //|BA_REGULAR
+    }
+
+    /*
+      ME: public abstract boolean checkAccess(File f, boolean write);
+      SE: public abstract boolean checkAccess(File f, int access);
+    */
+
+    public long getLastModifiedTime(File f) {
+        return fs.getLastModifiedTime(f);
+    }
+
+    public long getLength(File f) {
+        return fs.getLength(f);
+    }
+
+    /*
+      SE only
+      public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly);
+    */
+
+    public boolean createFileExclusively(String path) throws IOException {
+        return fs.createFileExclusively(path);
+    }
+
+    /*
+      ME only
+      public abstract boolean deleteOnExit(File f);
+    */
+
+    /*
+      SE only
+      public abstract long getSpace(File f, int t);
+    */
+
+    public boolean delete(File f) {
+        return fs.delete(f);
+    }
+
+    public String[] list(File f) {
+        return fs.list(f);
+    }
+
+    public boolean createDirectory(File f) {
+        return fs.createDirectory(f);
+    }
+
+    public boolean rename(File f1, File f2) {
+        return fs.rename(f1, f2);
+    }
+
+    public boolean setLastModifiedTime(File f, long time) {
+        return fs.setLastModifiedTime(f, time);
+    }
+
+    public boolean setReadOnly(File f) {
+        return fs.setReadOnly(f);
+    }
+
+    public File[] listRoots() {
+        return fs.listRoots();
+    }
+
+    /*
+      SE only
+      public abstract long getSpace(File f, int t);
+    */
+
+    public int compare(File f1, File f2) {
+        return fs.compare(f1, f2);
+    }
+
+    public int hashCode(File f) {
+        return fs.hashCode(f);
+    }
+}
diff --git a/src/libbluray/bdj/java/org/videolan/BDJClassLoader.java b/src/libbluray/bdj/java/org/videolan/BDJClassLoader.java
index f898cb9..e7385aa 100644
--- a/src/libbluray/bdj/java/org/videolan/BDJClassLoader.java
+++ b/src/libbluray/bdj/java/org/videolan/BDJClassLoader.java
@@ -110,5 +110,17 @@ public class BDJClassLoader extends URLClassLoader {
         this.xletClass = xletClass;
     }
 
+    public Class loadClass(String name) throws java.lang.ClassNotFoundException {
+        /* hook FileSystem in java.io.File */
+        if (name.equals("java.io.File")) {
+            Class c = super.loadClass(name);
+            if (c != null) {
+                java.io.BDFileSystem.init(c);
+            }
+            return c;
+        }
+        return super.loadClass(name);
+    }
+
     private String xletClass;
 }



More information about the libbluray-devel mailing list