public static void main(String args[]) { // 针对抽象构件编程 AbstractFile file1, file2, file3, file4, file5, folder1, folder2, folder3, folder4; folder1 = new Folder("Sunny的资料"); folder2 = new Folder("图像文件"); folder3 = new Folder("文本文件"); folder4 = new Folder("视频文件"); file1 = new ImageFile("小龙女.jpg"); file2 = new ImageFile("张无忌.gif"); file3 = new TextFile("九阴真经.txt"); file4 = new TextFile("葵花宝典.doc"); file5 = new VideoFile("笑傲江湖.rmvb"); folder2.add(file1); folder2.add(file2); folder3.add(file3); folder3.add(file4); folder4.add(file5); folder1.add(folder2); folder1.add(folder3); folder1.add(folder4); // 从“Sunny的资料”节点开始进行杀毒操作 folder1.killVirus(); }
public String list() { StringBuilder sb = new StringBuilder(); for (AbstractFile af : children) sb.append(af.list()); return sb.toString(); }
public String showStructure(int numTabs) { StringBuilder sb = new StringBuilder(); for (AbstractFile f : fileList) { for (int i = 0; i < numTabs; i++) { sb.append("\t"); } sb.append(f.toString()); if (f instanceof Directory) { sb.append(((Directory) f).showStructure(numTabs + 1)); } } return sb.toString(); }
/** * Creates a new SimpleFileAttributes instance whose attributes are set to those of the given * AbstractFile. Note that the path attribute is set to the file's {@link * com.mucommander.commons.file.AbstractFile#getAbsolutePath() absolute path}. * * @param file the file from which to fetch the attribute values */ public SimpleFileAttributes(AbstractFile file) { setPath(file.getAbsolutePath()); setExists(file.exists()); setDate(file.getDate()); setSize(file.getSize()); setDirectory(file.isDirectory()); setPermissions(file.getPermissions()); setOwner(file.getOwner()); setGroup(file.getGroup()); }
@Override public final boolean isFileOperationSupported(FileOperation op) { Class<? extends AbstractFile> thisClass = getClass(); Method opMethod = op.getCorrespondingMethod(thisClass); // If the method corresponding to the file operation has been overridden by this class (a // ProxyFile subclass), // check the presence of the UnsupportedFileOperation annotation in this class. try { if (!thisClass .getMethod(opMethod.getName(), opMethod.getParameterTypes()) .getDeclaringClass() .equals(ProxyFile.class)) { return AbstractFile.isFileOperationSupported(op, thisClass); } } catch (Exception e) { // Should never happen, unless AbstractFile method signatures have changed and FileOperation // has not been updated LOGGER.warn("Exception caught, this should not have happened", e); } // Otherwise, check for the presence of the UnsupportedFileOperation annotation in the wrapped // AbstractFile. return file.isFileOperationSupported(op); }
@Override public short getReplication() throws UnsupportedFileOperationException { return file.getReplication(); }
@Override public long getBlocksize() throws UnsupportedFileOperationException { return file.getBlocksize(); }
@Override public AbstractFile getRoot() { return file.getRoot(); }
public String toString() { return file.toString(); }
@Override public long getSize() { return file.getSize(); }
public String getContentType() { return AbstractFile.getContentType(file); }
@Override public void mkfile() throws IOException { file.mkfile(); }
@Override public long getDate() { return file.getDate(); }
@Override public void copyStream(InputStream in, boolean append, long length) throws FileTransferException { file.copyStream(in, append, length); }
@Override public AbstractFile[] ls(FilenameFilter filter) throws IOException { return file.ls(filter); }
@Override public InputStream getInputStream(long offset) throws IOException { return file.getInputStream(offset); }
@Override public AbstractFile getVolume() { return file.getVolume(); }
@Override public boolean isRoot() { return file.isRoot(); }
@Override public void changeReplication(short replication) throws IOException { file.changeReplication(replication); }
@Override public void deleteRecursively() throws IOException { file.deleteRecursively(); }
/** * Creates a new ProxyFile using the given file to delegate AbstractFile method calls to. * * @param file the file to be proxied */ public ProxyFile(AbstractFile file) { super(file.getURL()); this.file = file; }
public boolean equals(Object f) { return file.equals(f); }
@Override public void changeDate(long lastModified) throws IOException { file.changeDate(lastModified); }
/** * Copies recursively the given file or folder. * * @param file the file or folder to move * @param recurseParams destination folder where the given file will be copied (null for top level * files) * @return <code>true</code> if the file has been copied. */ @Override protected boolean processFile(AbstractFile file, Object recurseParams) { // Stop if interrupted if (getState() == INTERRUPTED) return false; // Destination folder AbstractFile destFolder = recurseParams == null ? baseDestFolder : (AbstractFile) recurseParams; // Is current file in base folder ? boolean isFileInBaseFolder = files.indexOf(file) != -1; // Determine filename in destination String destFileName; if (isFileInBaseFolder && newName != null) destFileName = newName; else destFileName = file.getName(); // Create destination AbstractFile instance AbstractFile destFile = createDestinationFile(destFolder, destFileName); if (destFile == null) return false; currentDestFile = destFile; // Do nothing if file is a symlink (skip file and return) if (file.isSymlink()) return true; destFile = checkForCollision(file, destFolder, destFile, false); if (destFile == null) return false; // Copy directory recursively if (file.isDirectory()) { // Create the folder in the destination folder if it doesn't exist if (!(destFile.exists() && destFile.isDirectory())) { // Loop for retry do { try { destFile.mkdir(); } catch (IOException e) { // Unable to create folder int ret = showErrorDialog( errorDialogTitle, Translator.get("cannot_create_folder", destFileName)); // Retry loops if (ret == RETRY_ACTION) continue; // Cancel or close dialog return false return false; // Skip continues } break; } while (true); } // and copy each file in this folder recursively do { // Loop for retry try { // for each file in folder... AbstractFile subFiles[] = file.ls(); // filesDiscovered(subFiles); for (int i = 0; i < subFiles.length && getState() != INTERRUPTED; i++) { // Notify job that we're starting to process this file (needed for recursive calls to // processFile) nextFile(subFiles[i]); processFile(subFiles[i], destFile); } // Set currentDestFile back to the enclosing folder in case an overridden processFile // method // needs to work with the folder after calling super.processFile. currentDestFile = destFile; // Only when finished with folder, set destination folder's date to match the original // folder one if (destFile.isFileOperationSupported(FileOperation.CHANGE_DATE)) { try { destFile.changeDate(file.getDate()); } catch (IOException e) { AppLogger.fine("failed to change the date of " + destFile, e); // Fail silently } } return true; } catch (IOException e) { // file.ls() failed int ret = showErrorDialog( errorDialogTitle, Translator.get("cannot_read_folder", file.getName())); // Retry loops if (ret == RETRY_ACTION) continue; // Cancel, skip or close dialog returns false return false; } } while (true); } // File is a regular file, copy it else { // Copy the file return tryCopyFile(file, destFile, append, errorDialogTitle); } }
@Override public AbstractFile getParent() { return file.getParent(); }
@Override public PermissionBits getChangeablePermissions() { return file.getChangeablePermissions(); }
public int hashCode() { return file.hashCode(); }
@Override public String getPermissionsString() { return file.getPermissionsString(); }
@Override public boolean equalsCanonical(Object f) { return file.equalsCanonical(f); }
@Override public void changePermissions(int permissions) throws IOException { file.changePermissions(permissions); }