rt/emul/compact/src/main/java/java/io/File.java
changeset 1260 fe3567c7b522
parent 1259 d257b7a37635
     1.1 --- a/rt/emul/compact/src/main/java/java/io/File.java	Sat Sep 07 13:55:09 2013 +0200
     1.2 +++ b/rt/emul/compact/src/main/java/java/io/File.java	Sat Sep 07 13:56:22 2013 +0200
     1.3 @@ -29,13 +29,6 @@
     1.4  import java.net.URL;
     1.5  import java.net.MalformedURLException;
     1.6  import java.net.URISyntaxException;
     1.7 -import java.util.List;
     1.8 -import java.util.ArrayList;
     1.9 -import java.security.AccessController;
    1.10 -import java.security.SecureRandom;
    1.11 -import java.nio.file.Path;
    1.12 -import java.nio.file.FileSystems;
    1.13 -import sun.security.action.GetPropertyAction;
    1.14  
    1.15  /**
    1.16   * An abstract representation of file and directory pathnames.
    1.17 @@ -153,7 +146,50 @@
    1.18      /**
    1.19       * The FileSystem object representing the platform's local file system.
    1.20       */
    1.21 -    static private FileSystem fs = FileSystem.getFileSystem();
    1.22 +    static private FileSystem fs = new FileSystem();
    1.23 +    private static class FileSystem {
    1.24 +
    1.25 +        private char getSeparator() {
    1.26 +            return '/';
    1.27 +        }
    1.28 +
    1.29 +        private String resolve(String path, String child) {
    1.30 +            return path + '/' + child;
    1.31 +        }
    1.32 +
    1.33 +        private String normalize(String pathname) {
    1.34 +            return pathname;
    1.35 +        }
    1.36 +
    1.37 +        private int prefixLength(String path) {
    1.38 +            return 0;
    1.39 +        }
    1.40 +
    1.41 +        private String getDefaultParent() {
    1.42 +            return "/";
    1.43 +        }
    1.44 +
    1.45 +        private String fromURIPath(String p) {
    1.46 +            return p;
    1.47 +        }
    1.48 +
    1.49 +        private boolean isAbsolute(File aThis) {
    1.50 +            return aThis.getPath().startsWith("/");
    1.51 +        }
    1.52 +
    1.53 +        private int compare(File one, File two) {
    1.54 +            return one.getPath().compareTo(two.getPath());
    1.55 +        }
    1.56 +
    1.57 +        private int hashCode(File aThis) {
    1.58 +            return aThis.getPath().hashCode();
    1.59 +        }
    1.60 +
    1.61 +        private char getPathSeparator() {
    1.62 +            return ':';
    1.63 +        }
    1.64 +        
    1.65 +    }
    1.66  
    1.67      /**
    1.68       * This abstract pathname's normalized pathname string.  A normalized
    1.69 @@ -527,7 +563,7 @@
    1.70       * @see     java.io.File#isAbsolute()
    1.71       */
    1.72      public String getAbsolutePath() {
    1.73 -        return fs.resolve(this);
    1.74 +        throw new SecurityException();
    1.75      }
    1.76  
    1.77      /**
    1.78 @@ -586,7 +622,7 @@
    1.79       * @see     Path#toRealPath
    1.80       */
    1.81      public String getCanonicalPath() throws IOException {
    1.82 -        return fs.canonicalize(fs.resolve(this));
    1.83 +        throw new SecurityException();
    1.84      }
    1.85  
    1.86      /**
    1.87 @@ -723,11 +759,7 @@
    1.88       *          method denies read access to the file
    1.89       */
    1.90      public boolean canRead() {
    1.91 -        SecurityManager security = System.getSecurityManager();
    1.92 -        if (security != null) {
    1.93 -            security.checkRead(path);
    1.94 -        }
    1.95 -        return fs.checkAccess(this, FileSystem.ACCESS_READ);
    1.96 +        throw new SecurityException();
    1.97      }
    1.98  
    1.99      /**
   1.100 @@ -745,11 +777,7 @@
   1.101       *          method denies write access to the file
   1.102       */
   1.103      public boolean canWrite() {
   1.104 -        SecurityManager security = System.getSecurityManager();
   1.105 -        if (security != null) {
   1.106 -            security.checkWrite(path);
   1.107 -        }
   1.108 -        return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
   1.109 +        throw new SecurityException();
   1.110      }
   1.111  
   1.112      /**
   1.113 @@ -765,11 +793,7 @@
   1.114       *          method denies read access to the file or directory
   1.115       */
   1.116      public boolean exists() {
   1.117 -        SecurityManager security = System.getSecurityManager();
   1.118 -        if (security != null) {
   1.119 -            security.checkRead(path);
   1.120 -        }
   1.121 -        return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
   1.122 +        throw new SecurityException();
   1.123      }
   1.124  
   1.125      /**
   1.126 @@ -792,12 +816,7 @@
   1.127       *          method denies read access to the file
   1.128       */
   1.129      public boolean isDirectory() {
   1.130 -        SecurityManager security = System.getSecurityManager();
   1.131 -        if (security != null) {
   1.132 -            security.checkRead(path);
   1.133 -        }
   1.134 -        return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
   1.135 -                != 0);
   1.136 +        throw new SecurityException();
   1.137      }
   1.138  
   1.139      /**
   1.140 @@ -822,11 +841,7 @@
   1.141       *          method denies read access to the file
   1.142       */
   1.143      public boolean isFile() {
   1.144 -        SecurityManager security = System.getSecurityManager();
   1.145 -        if (security != null) {
   1.146 -            security.checkRead(path);
   1.147 -        }
   1.148 -        return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);
   1.149 +        throw new SecurityException();
   1.150      }
   1.151  
   1.152      /**
   1.153 @@ -848,11 +863,7 @@
   1.154       * @since 1.2
   1.155       */
   1.156      public boolean isHidden() {
   1.157 -        SecurityManager security = System.getSecurityManager();
   1.158 -        if (security != null) {
   1.159 -            security.checkRead(path);
   1.160 -        }
   1.161 -        return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
   1.162 +        throw new SecurityException();
   1.163      }
   1.164  
   1.165      /**
   1.166 @@ -877,11 +888,7 @@
   1.167       *          method denies read access to the file
   1.168       */
   1.169      public long lastModified() {
   1.170 -        SecurityManager security = System.getSecurityManager();
   1.171 -        if (security != null) {
   1.172 -            security.checkRead(path);
   1.173 -        }
   1.174 -        return fs.getLastModifiedTime(this);
   1.175 +        throw new SecurityException();
   1.176      }
   1.177  
   1.178      /**
   1.179 @@ -905,11 +912,7 @@
   1.180       *          method denies read access to the file
   1.181       */
   1.182      public long length() {
   1.183 -        SecurityManager security = System.getSecurityManager();
   1.184 -        if (security != null) {
   1.185 -            security.checkRead(path);
   1.186 -        }
   1.187 -        return fs.getLength(this);
   1.188 +        throw new SecurityException();
   1.189      }
   1.190  
   1.191  
   1.192 @@ -942,9 +945,7 @@
   1.193       * @since 1.2
   1.194       */
   1.195      public boolean createNewFile() throws IOException {
   1.196 -        SecurityManager security = System.getSecurityManager();
   1.197 -        if (security != null) security.checkWrite(path);
   1.198 -        return fs.createFileExclusively(path);
   1.199 +        throw new SecurityException();
   1.200      }
   1.201  
   1.202      /**
   1.203 @@ -966,11 +967,7 @@
   1.204       *          delete access to the file
   1.205       */
   1.206      public boolean delete() {
   1.207 -        SecurityManager security = System.getSecurityManager();
   1.208 -        if (security != null) {
   1.209 -            security.checkDelete(path);
   1.210 -        }
   1.211 -        return fs.delete(this);
   1.212 +        throw new SecurityException();
   1.213      }
   1.214  
   1.215      /**
   1.216 @@ -1001,11 +998,7 @@
   1.217       * @since 1.2
   1.218       */
   1.219      public void deleteOnExit() {
   1.220 -        SecurityManager security = System.getSecurityManager();
   1.221 -        if (security != null) {
   1.222 -            security.checkDelete(path);
   1.223 -        }
   1.224 -        DeleteOnExitHook.add(path);
   1.225 +        throw new SecurityException();
   1.226      }
   1.227  
   1.228      /**
   1.229 @@ -1041,11 +1034,7 @@
   1.230       *          the directory
   1.231       */
   1.232      public String[] list() {
   1.233 -        SecurityManager security = System.getSecurityManager();
   1.234 -        if (security != null) {
   1.235 -            security.checkRead(path);
   1.236 -        }
   1.237 -        return fs.list(this);
   1.238 +        throw new SecurityException();
   1.239      }
   1.240  
   1.241      /**
   1.242 @@ -1078,17 +1067,7 @@
   1.243       * @see java.nio.file.Files#newDirectoryStream(Path,String)
   1.244       */
   1.245      public String[] list(FilenameFilter filter) {
   1.246 -        String names[] = list();
   1.247 -        if ((names == null) || (filter == null)) {
   1.248 -            return names;
   1.249 -        }
   1.250 -        List<String> v = new ArrayList<>();
   1.251 -        for (int i = 0 ; i < names.length ; i++) {
   1.252 -            if (filter.accept(this, names[i])) {
   1.253 -                v.add(names[i]);
   1.254 -            }
   1.255 -        }
   1.256 -        return v.toArray(new String[v.size()]);
   1.257 +        throw new SecurityException();
   1.258      }
   1.259  
   1.260      /**
   1.261 @@ -1130,14 +1109,7 @@
   1.262       * @since  1.2
   1.263       */
   1.264      public File[] listFiles() {
   1.265 -        String[] ss = list();
   1.266 -        if (ss == null) return null;
   1.267 -        int n = ss.length;
   1.268 -        File[] fs = new File[n];
   1.269 -        for (int i = 0; i < n; i++) {
   1.270 -            fs[i] = new File(ss[i], this);
   1.271 -        }
   1.272 -        return fs;
   1.273 +        throw new SecurityException();
   1.274      }
   1.275  
   1.276      /**
   1.277 @@ -1171,13 +1143,7 @@
   1.278       * @see java.nio.file.Files#newDirectoryStream(Path,String)
   1.279       */
   1.280      public File[] listFiles(FilenameFilter filter) {
   1.281 -        String ss[] = list();
   1.282 -        if (ss == null) return null;
   1.283 -        ArrayList<File> files = new ArrayList<>();
   1.284 -        for (String s : ss)
   1.285 -            if ((filter == null) || filter.accept(this, s))
   1.286 -                files.add(new File(s, this));
   1.287 -        return files.toArray(new File[files.size()]);
   1.288 +        throw new SecurityException();
   1.289      }
   1.290  
   1.291      /**
   1.292 @@ -1209,15 +1175,7 @@
   1.293       * @see java.nio.file.Files#newDirectoryStream(Path,java.nio.file.DirectoryStream.Filter)
   1.294       */
   1.295      public File[] listFiles(FileFilter filter) {
   1.296 -        String ss[] = list();
   1.297 -        if (ss == null) return null;
   1.298 -        ArrayList<File> files = new ArrayList<>();
   1.299 -        for (String s : ss) {
   1.300 -            File f = new File(s, this);
   1.301 -            if ((filter == null) || filter.accept(f))
   1.302 -                files.add(f);
   1.303 -        }
   1.304 -        return files.toArray(new File[files.size()]);
   1.305 +        throw new SecurityException();
   1.306      }
   1.307  
   1.308      /**
   1.309 @@ -1232,11 +1190,7 @@
   1.310       *          method does not permit the named directory to be created
   1.311       */
   1.312      public boolean mkdir() {
   1.313 -        SecurityManager security = System.getSecurityManager();
   1.314 -        if (security != null) {
   1.315 -            security.checkWrite(path);
   1.316 -        }
   1.317 -        return fs.createDirectory(this);
   1.318 +        throw new SecurityException();
   1.319      }
   1.320  
   1.321      /**
   1.322 @@ -1260,22 +1214,7 @@
   1.323       *          parent directories to be created
   1.324       */
   1.325      public boolean mkdirs() {
   1.326 -        if (exists()) {
   1.327 -            return false;
   1.328 -        }
   1.329 -        if (mkdir()) {
   1.330 -            return true;
   1.331 -        }
   1.332 -        File canonFile = null;
   1.333 -        try {
   1.334 -            canonFile = getCanonicalFile();
   1.335 -        } catch (IOException e) {
   1.336 -            return false;
   1.337 -        }
   1.338 -
   1.339 -        File parent = canonFile.getParentFile();
   1.340 -        return (parent != null && (parent.mkdirs() || parent.exists()) &&
   1.341 -                canonFile.mkdir());
   1.342 +        throw new SecurityException();
   1.343      }
   1.344  
   1.345      /**
   1.346 @@ -1306,12 +1245,7 @@
   1.347       *          If parameter <code>dest</code> is <code>null</code>
   1.348       */
   1.349      public boolean renameTo(File dest) {
   1.350 -        SecurityManager security = System.getSecurityManager();
   1.351 -        if (security != null) {
   1.352 -            security.checkWrite(path);
   1.353 -            security.checkWrite(dest.path);
   1.354 -        }
   1.355 -        return fs.rename(this, dest);
   1.356 +        throw new SecurityException();
   1.357      }
   1.358  
   1.359      /**
   1.360 @@ -1341,12 +1275,7 @@
   1.361       * @since 1.2
   1.362       */
   1.363      public boolean setLastModified(long time) {
   1.364 -        if (time < 0) throw new IllegalArgumentException("Negative time");
   1.365 -        SecurityManager security = System.getSecurityManager();
   1.366 -        if (security != null) {
   1.367 -            security.checkWrite(path);
   1.368 -        }
   1.369 -        return fs.setLastModifiedTime(this, time);
   1.370 +        throw new SecurityException();
   1.371      }
   1.372  
   1.373      /**
   1.374 @@ -1367,11 +1296,7 @@
   1.375       * @since 1.2
   1.376       */
   1.377      public boolean setReadOnly() {
   1.378 -        SecurityManager security = System.getSecurityManager();
   1.379 -        if (security != null) {
   1.380 -            security.checkWrite(path);
   1.381 -        }
   1.382 -        return fs.setReadOnly(this);
   1.383 +        throw new SecurityException();
   1.384      }
   1.385  
   1.386      /**
   1.387 @@ -1405,11 +1330,7 @@
   1.388       * @since 1.6
   1.389       */
   1.390      public boolean setWritable(boolean writable, boolean ownerOnly) {
   1.391 -        SecurityManager security = System.getSecurityManager();
   1.392 -        if (security != null) {
   1.393 -            security.checkWrite(path);
   1.394 -        }
   1.395 -        return fs.setPermission(this, FileSystem.ACCESS_WRITE, writable, ownerOnly);
   1.396 +        throw new SecurityException();
   1.397      }
   1.398  
   1.399      /**
   1.400 @@ -1475,11 +1396,7 @@
   1.401       * @since 1.6
   1.402       */
   1.403      public boolean setReadable(boolean readable, boolean ownerOnly) {
   1.404 -        SecurityManager security = System.getSecurityManager();
   1.405 -        if (security != null) {
   1.406 -            security.checkWrite(path);
   1.407 -        }
   1.408 -        return fs.setPermission(this, FileSystem.ACCESS_READ, readable, ownerOnly);
   1.409 +        throw new SecurityException();
   1.410      }
   1.411  
   1.412      /**
   1.413 @@ -1548,11 +1465,7 @@
   1.414       * @since 1.6
   1.415       */
   1.416      public boolean setExecutable(boolean executable, boolean ownerOnly) {
   1.417 -        SecurityManager security = System.getSecurityManager();
   1.418 -        if (security != null) {
   1.419 -            security.checkWrite(path);
   1.420 -        }
   1.421 -        return fs.setPermission(this, FileSystem.ACCESS_EXECUTE, executable, ownerOnly);
   1.422 +        throw new SecurityException();
   1.423      }
   1.424  
   1.425      /**
   1.426 @@ -1602,11 +1515,7 @@
   1.427       * @since 1.6
   1.428       */
   1.429      public boolean canExecute() {
   1.430 -        SecurityManager security = System.getSecurityManager();
   1.431 -        if (security != null) {
   1.432 -            security.checkExec(path);
   1.433 -        }
   1.434 -        return fs.checkAccess(this, FileSystem.ACCESS_EXECUTE);
   1.435 +        throw new SecurityException();
   1.436      }
   1.437  
   1.438  
   1.439 @@ -1655,7 +1564,7 @@
   1.440       * @see java.nio.file.FileStore
   1.441       */
   1.442      public static File[] listRoots() {
   1.443 -        return fs.listRoots();
   1.444 +        throw new SecurityException();
   1.445      }
   1.446  
   1.447  
   1.448 @@ -1677,12 +1586,7 @@
   1.449       * @since  1.6
   1.450       */
   1.451      public long getTotalSpace() {
   1.452 -        SecurityManager sm = System.getSecurityManager();
   1.453 -        if (sm != null) {
   1.454 -            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
   1.455 -            sm.checkRead(path);
   1.456 -        }
   1.457 -        return fs.getSpace(this, FileSystem.SPACE_TOTAL);
   1.458 +        throw new SecurityException();
   1.459      }
   1.460  
   1.461      /**
   1.462 @@ -1712,12 +1616,7 @@
   1.463       * @since  1.6
   1.464       */
   1.465      public long getFreeSpace() {
   1.466 -        SecurityManager sm = System.getSecurityManager();
   1.467 -        if (sm != null) {
   1.468 -            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
   1.469 -            sm.checkRead(path);
   1.470 -        }
   1.471 -        return fs.getSpace(this, FileSystem.SPACE_FREE);
   1.472 +        throw new SecurityException();
   1.473      }
   1.474  
   1.475      /**
   1.476 @@ -1750,38 +1649,11 @@
   1.477       * @since  1.6
   1.478       */
   1.479      public long getUsableSpace() {
   1.480 -        SecurityManager sm = System.getSecurityManager();
   1.481 -        if (sm != null) {
   1.482 -            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
   1.483 -            sm.checkRead(path);
   1.484 -        }
   1.485 -        return fs.getSpace(this, FileSystem.SPACE_USABLE);
   1.486 +        throw new SecurityException();
   1.487      }
   1.488  
   1.489      /* -- Temporary files -- */
   1.490  
   1.491 -    private static class TempDirectory {
   1.492 -        private TempDirectory() { }
   1.493 -
   1.494 -        // temporary directory location
   1.495 -        private static final File tmpdir = new File(fs.normalize(AccessController
   1.496 -            .doPrivileged(new GetPropertyAction("java.io.tmpdir"))));
   1.497 -        static File location() {
   1.498 -            return tmpdir;
   1.499 -        }
   1.500 -
   1.501 -        // file name generation
   1.502 -        private static final SecureRandom random = new SecureRandom();
   1.503 -        static File generateFile(String prefix, String suffix, File dir) {
   1.504 -            long n = random.nextLong();
   1.505 -            if (n == Long.MIN_VALUE) {
   1.506 -                n = 0;      // corner case
   1.507 -            } else {
   1.508 -                n = Math.abs(n);
   1.509 -            }
   1.510 -            return new File(dir, prefix + Long.toString(n) + suffix);
   1.511 -        }
   1.512 -    }
   1.513  
   1.514      /**
   1.515       * <p> Creates a new empty file in the specified directory, using the
   1.516 @@ -1856,28 +1728,7 @@
   1.517                                        File directory)
   1.518          throws IOException
   1.519      {
   1.520 -        if (prefix.length() < 3)
   1.521 -            throw new IllegalArgumentException("Prefix string too short");
   1.522 -        if (suffix == null)
   1.523 -            suffix = ".tmp";
   1.524 -
   1.525 -        File tmpdir = (directory != null) ? directory : TempDirectory.location();
   1.526 -        SecurityManager sm = System.getSecurityManager();
   1.527 -        File f;
   1.528 -        do {
   1.529 -            f = TempDirectory.generateFile(prefix, suffix, tmpdir);
   1.530 -            if (sm != null) {
   1.531 -                try {
   1.532 -                    sm.checkWrite(f.getPath());
   1.533 -                } catch (SecurityException se) {
   1.534 -                    // don't reveal temporary directory location
   1.535 -                    if (directory == null)
   1.536 -                        throw new SecurityException("Unable to create temporary file");
   1.537 -                    throw se;
   1.538 -                }
   1.539 -            }
   1.540 -        } while (!fs.createFileExclusively(f.getPath()));
   1.541 -        return f;
   1.542 +        throw new SecurityException();
   1.543      }
   1.544  
   1.545      /**
   1.546 @@ -2031,7 +1882,7 @@
   1.547      private static final long serialVersionUID = 301077366599181567L;
   1.548  
   1.549      // -- Integration with java.nio.file --
   1.550 -
   1.551 +/*
   1.552      private volatile transient Path filePath;
   1.553  
   1.554      /**
   1.555 @@ -2060,17 +1911,17 @@
   1.556       * @since   1.7
   1.557       * @see Path#toFile
   1.558       */
   1.559 -    public Path toPath() {
   1.560 -        Path result = filePath;
   1.561 -        if (result == null) {
   1.562 -            synchronized (this) {
   1.563 -                result = filePath;
   1.564 -                if (result == null) {
   1.565 -                    result = FileSystems.getDefault().getPath(path);
   1.566 -                    filePath = result;
   1.567 -                }
   1.568 -            }
   1.569 -        }
   1.570 -        return result;
   1.571 -    }
   1.572 +//    public Path toPath() {
   1.573 +//        Path result = filePath;
   1.574 +//        if (result == null) {
   1.575 +//            synchronized (this) {
   1.576 +//                result = filePath;
   1.577 +//                if (result == null) {
   1.578 +//                    result = FileSystems.getDefault().getPath(path);
   1.579 +//                    filePath = result;
   1.580 +//                }
   1.581 +//            }
   1.582 +//        }
   1.583 +//        return result;
   1.584 +//    }
   1.585  }