abstractfilestore.java
来自「world wind java sdk 源码」· Java 代码 · 共 818 行 · 第 1/2 页
JAVA
818 行
StoreLocation oldLocation = this.storeLocationFor(newPath); if (oldLocation != null) this.readLocations.remove(oldLocation); if (index > 0 && index > this.readLocations.size()) index = this.readLocations.size(); java.io.File newFile = new java.io.File(newPath); StoreLocation newLocation = new StoreLocation(newFile, isInstall); this.readLocations.add(index, newLocation); } public void removeLocation(String path) { if (path == null || path.length() == 0) { String message = Logging.getMessage("nullValue.FileStorePathIsNull"); Logging.logger().severe(message); // Just warn and return. return; } StoreLocation location = this.storeLocationFor(path); if (location == null) // Path is not part of this FileStore. return; if (location.equals(this.writeLocation)) { String message = Logging.getMessage("FileStore.CannotRemoveWriteLocation", path); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.readLocations.remove(location); } public boolean isInstallLocation(String path) { if (path == null || path.length() == 0) { String message = Logging.getMessage("nullValue.FileStorePathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } StoreLocation location = this.storeLocationFor(path); return location != null && location.isInstall(); } protected StoreLocation storeLocationFor(String path) { java.io.File file = new java.io.File(path); for (StoreLocation location : this.readLocations) { if (file.equals(location.getFile())) return location; } return null; } //**************************************************************// //******************** File Store Contents *******************// //**************************************************************// public boolean containsFile(String fileName) { if (fileName == null) return false; for (StoreLocation location : this.readLocations) { java.io.File dir = location.getFile(); java.io.File file; if (fileName.startsWith(dir.getAbsolutePath())) file = new java.io.File(fileName); else file = makeAbsoluteFile(dir, fileName); if (file.exists()) return true; } return false; } /** * @param fileName the name of the file to find * @param checkClassPath if <code>true</code>, the class path is first searched for the file, otherwise the class * path is not searched unless it's one of the explicit paths in the cache search directories * * @return a handle to the requested file if it exists in the cache, otherwise null * * @throws IllegalArgumentException if <code>fileName</code> is null */ public java.net.URL findFile(String fileName, boolean checkClassPath) { if (fileName == null) { String message = Logging.getMessage("nullValue.FilePathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (checkClassPath) { java.net.URL url = this.getClass().getClassLoader().getResource(fileName); if (url != null) return url; } for (StoreLocation location : this.readLocations) { java.io.File dir = location.getFile(); if (!dir.exists()) continue; java.io.File file = new java.io.File(makeAbsolutePath(dir, fileName)); if (file.exists()) { try { if (location.isMarkWhenUsed()) markFileUsed(file); else markFileUsed(file.getParentFile()); return file.toURI().toURL(); } catch (java.net.MalformedURLException e) { Logging.logger().log(Level.SEVERE, Logging.getMessage("FileStore.ExceptionCreatingURLForFile", file.getPath()), e); } } } return null; } @SuppressWarnings({"ResultOfMethodCallIgnored"}) protected static void markFileUsed(File file) { if (file == null) return; long currentTime = System.currentTimeMillis(); if (file.canWrite()) file.setLastModified(currentTime); if (file.isDirectory()) return; File parent = file.getParentFile(); if (parent != null && parent.canWrite()) parent.setLastModified(currentTime); } /** * @param fileName the name to give the newly created file * * @return a handle to the newly created file if it could be created and added to the file store, otherwise null * * @throws IllegalArgumentException if <code>fileName</code> is null */ public java.io.File newFile(String fileName) { if (fileName == null) { String message = Logging.getMessage("nullValue.FilePathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (this.writeLocation != null) { String fullPath = makeAbsolutePath(this.writeLocation.getFile(), fileName); java.io.File file = new java.io.File(fullPath); boolean canCreateFile = false; // This block of code must be synchronized for proper operation. A thread may check that // file.getParentFile() does not exist, and become immediately suspended. A second thread may then create // the parent and ancestor directories. When the first thread wakes up, file.getParentFile().mkdirs() will // fail, resulting in an erroneous log message: The log will report that the file cannot be created. synchronized (this.fileLock) { if (file.getParentFile().exists()) canCreateFile = true; else if (file.getParentFile().mkdirs()) canCreateFile = true; } if (canCreateFile) return file; else { String msg = Logging.getMessage("generic.CannotCreateFile", fullPath); Logging.logger().severe(msg); } } return null; } /** * @param url the "file:" URL of the file to remove from the file store * * @throws IllegalArgumentException if <code>url</code> is null */ @SuppressWarnings({"ResultOfMethodCallIgnored"}) public void removeFile(java.net.URL url) { if (url == null) { String msg = Logging.getMessage("nullValue.URLIsNull"); Logging.logger().severe(msg); throw new IllegalArgumentException(msg); } try { java.io.File file = new java.io.File(url.toURI()); // This block of code must be synchronized for proper operation. A thread may check that the file exists, // and become immediately suspended. A second thread may then delete that file. When the first thread // wakes up, file.delete() will fail. synchronized (this.fileLock) { if (file.exists()) file.delete(); } } catch (java.net.URISyntaxException e) { Logging.logger().log(Level.SEVERE, Logging.getMessage("FileStore.ExceptionRemovingFile", url.toString()), e); } } protected static java.io.File makeAbsoluteFile(java.io.File file, String fileName) { return new java.io.File(file.getAbsolutePath() + "/" + fileName); } protected static String makeAbsolutePath(java.io.File dir, String fileName) { return dir.getAbsolutePath() + "/" + fileName; } protected static String storePathForFile(StoreLocation location, java.io.File file) { String path = file.getPath(); if (location != null) { String locationPath = location.getFile().getPath(); if (path.startsWith(locationPath)) path = path.substring(locationPath.length(), path.length()); } return path; } //**************************************************************// //******************** Data Descriptors **********************// //**************************************************************// public java.util.List<? extends DataDescriptor> findAllDataDescriptors() { java.util.ArrayList<DataDescriptor> descriptors = new java.util.ArrayList<DataDescriptor>(); for (StoreLocation location : this.readLocations) { this.doFindDataDescriptors(location, location.getFile(), descriptors); } return descriptors; } public java.util.List<? extends DataDescriptor> findDataDescriptors(String path) { if (path == null || path.length() == 0) { String message = Logging.getMessage("nullValue.FileStorePathIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } java.util.ArrayList<DataDescriptor> descriptors = new java.util.ArrayList<DataDescriptor>(); StoreLocation location = this.storeLocationFor(path); if (location != null) this.doFindDataDescriptors(location, location.getFile(), descriptors); return descriptors; } protected void doFindDataDescriptors(StoreLocation location, java.io.File dir, java.util.List<DataDescriptor> descriptors) { if (!dir.exists()) return; if (!dir.isDirectory()) return; // Find all files ending with ".xml" in the specified directory. java.io.File[] childFiles = dir.listFiles(new java.io.FileFilter() { public boolean accept(java.io.File file) { return file != null && file.exists() && !file.isDirectory(); } }); // Search this level of the file store for a valid DataDescriptor. If one is found, we stop searching // this branch of the file store. This has the effect of choosing the descriptor closest to the file store root. for (java.io.File childFile : childFiles) { if (this.readDataDescriptor(location, childFile, descriptors)) return; } java.io.File[] childDirs = dir.listFiles(new java.io.FileFilter() { public boolean accept(java.io.File file) { return file != null && file.isDirectory(); } }); // No DataDescriptor was found at this level. Continue by independently searching each sub-directory. for (java.io.File childDir : childDirs) { this.doFindDataDescriptors(location, childDir, descriptors); } } protected boolean readDataDescriptor(StoreLocation location, java.io.File descriptorFile, java.util.List<DataDescriptor> descriptors) { DataDescriptorReader reader = null; DataDescriptor descriptor = null; // Search the DataDescriptorIORegistry for a DataDescriptorReader that can parse the specified URL. // We want to find the first match and assign it to 'reader'. Or if no match exists, we leave reader==null. Iterable<? extends DataDescriptorReader> readers = DataIORegistry.getInstance().createDataDescriptorReaders(); for (DataDescriptorReader r : readers) { try { r.setSource(descriptorFile); if (r.canRead()) { reader = r; break; } } catch (java.io.IOException e) { String message = Logging.getMessage("generic.ExceptionWhileReading", descriptorFile); Logging.logger().log(java.util.logging.Level.SEVERE, message, e); } } // We have a reader that can parse the specified location. if (reader != null) { try { reader.setSource(descriptorFile); descriptor = reader.read(); } catch (java.io.IOException e) { String message = Logging.getMessage("generic.ExceptionWhileReading", descriptorFile); Logging.logger().log(java.util.logging.Level.SEVERE, message, e); } } if (descriptor != null) { String storePath = storePathForFile(location, descriptorFile.getParentFile()); descriptor.setFileStoreLocation(location.getFile()); descriptor.setFileStorePath(storePath); descriptor.setInstalled(location.isInstall()); // Attach a URL reference to the descriptor. This property may be used by an application to determine // the descriptor's source location. try { descriptor.setValue(AVKey.URL, descriptorFile.toURI().toURL()); } catch (java.net.MalformedURLException e) { Logging.logger().log(Level.SEVERE, Logging.getMessage("FileStore.ExceptionCreatingURLForFile", descriptorFile.getPath()), e); } descriptors.add(descriptor); } // If we were able to parse the specified URL, then return true. Otherwise return false. return descriptor != null; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?