Пример #1
0
 private static void registerDefaultProviders() {
   if (providers == null || defaultProvider == null) {
     // 不使用硬编码包名字符串的方式,重命名包名时常常忘记
     String packageName = FilePath.class.getPackage().getName() + ".";
     Map<String, FilePath> map = Collections.synchronizedMap(New.<String, FilePath>hashMap());
     for (String c :
         new String[] {
           "FilePathDisk",
           "FilePathMem",
           "FilePathMemLZF",
           "FilePathNioMem",
           "FilePathNioMemLZF",
           "FilePathSplit",
           "FilePathNio",
           "FilePathNioMapped",
           "FilePathZip"
         }) {
       try {
         FilePath p = (FilePath) Class.forName(packageName + c).newInstance();
         map.put(p.getScheme(), p);
         if (defaultProvider == null) {
           defaultProvider = p;
         }
       } catch (Exception e) {
         // ignore - the files may be excluded in purpose
       }
     }
     providers = map;
   }
 }
Пример #2
0
 /**
  * Create a new temporary file.
  *
  * @param suffix the suffix
  * @param deleteOnExit if the file should be deleted when the virtual machine exists
  * @param inTempDir if the file should be stored in the temporary directory
  * @return the name of the created file
  */
 public FilePath createTempFile(String suffix, boolean deleteOnExit, boolean inTempDir)
     throws IOException {
   while (true) {
     FilePath p = getPath(name + getNextTempFileNamePart(false) + suffix);
     if (p.exists() || !p.createFile()) {
       // in theory, the random number could collide
       getNextTempFileNamePart(true);
       continue;
     }
     p.open("rw").close();
     return p;
   }
 }
Пример #3
0
 /**
  * Get the file path object for the given path. Windows-style '\' is replaced with '/'.
  *
  * @param path the path
  * @return the file path object
  */
 public static FilePath get(String path) {
   path = path.replace('\\', '/');
   int index = path.indexOf(':');
   registerDefaultProviders();
   if (index < 2) {
     // use the default provider if no prefix or
     // only a single character (drive name)
     return defaultProvider.getPath(path);
   }
   String scheme = path.substring(0, index);
   FilePath p = providers.get(scheme);
   if (p == null) {
     // provider not found - use the default
     p = defaultProvider;
   }
   return p.getPath(path);
 }
Пример #4
0
 /**
  * Register a file provider.
  *
  * @param provider the file provider
  */
 public static void register(FilePath provider) {
   registerDefaultProviders();
   providers.put(provider.getScheme(), provider);
 }
Пример #5
0
 /**
  * Unregister a file provider.
  *
  * @param provider the file provider
  */
 public static void unregister(FilePath provider) {
   registerDefaultProviders();
   providers.remove(provider.getScheme());
 }