コード例 #1
0
  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();
  }
コード例 #2
0
ファイル: CompositePattern.java プロジェクト: Suhotaro/Java
  public String list() {
    StringBuilder sb = new StringBuilder();

    for (AbstractFile af : children) sb.append(af.list());

    return sb.toString();
  }
コード例 #3
0
ファイル: Directory.java プロジェクト: av239/FileSystem
 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();
 }
コード例 #4
0
 /**
  * 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());
 }
コード例 #5
0
  @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);
  }
コード例 #6
0
 @Override
 public short getReplication() throws UnsupportedFileOperationException {
   return file.getReplication();
 }
コード例 #7
0
 @Override
 public long getBlocksize() throws UnsupportedFileOperationException {
   return file.getBlocksize();
 }
コード例 #8
0
 @Override
 public AbstractFile getRoot() {
   return file.getRoot();
 }
コード例 #9
0
 public String toString() {
   return file.toString();
 }
コード例 #10
0
 @Override
 public long getSize() {
   return file.getSize();
 }
コード例 #11
0
 public String getContentType() {
   return AbstractFile.getContentType(file);
 }
コード例 #12
0
 @Override
 public void mkfile() throws IOException {
   file.mkfile();
 }
コード例 #13
0
 @Override
 public long getDate() {
   return file.getDate();
 }
コード例 #14
0
 @Override
 public void copyStream(InputStream in, boolean append, long length) throws FileTransferException {
   file.copyStream(in, append, length);
 }
コード例 #15
0
 @Override
 public AbstractFile[] ls(FilenameFilter filter) throws IOException {
   return file.ls(filter);
 }
コード例 #16
0
 @Override
 public InputStream getInputStream(long offset) throws IOException {
   return file.getInputStream(offset);
 }
コード例 #17
0
 @Override
 public AbstractFile getVolume() {
   return file.getVolume();
 }
コード例 #18
0
 @Override
 public boolean isRoot() {
   return file.isRoot();
 }
コード例 #19
0
 @Override
 public void changeReplication(short replication) throws IOException {
   file.changeReplication(replication);
 }
コード例 #20
0
 @Override
 public void deleteRecursively() throws IOException {
   file.deleteRecursively();
 }
コード例 #21
0
 /**
  * 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;
 }
コード例 #22
0
 public boolean equals(Object f) {
   return file.equals(f);
 }
コード例 #23
0
 @Override
 public void changeDate(long lastModified) throws IOException {
   file.changeDate(lastModified);
 }
コード例 #24
0
ファイル: CopyJob.java プロジェクト: nuCommander/nuCommander
  /**
   * 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);
    }
  }
コード例 #25
0
 @Override
 public AbstractFile getParent() {
   return file.getParent();
 }
コード例 #26
0
 @Override
 public PermissionBits getChangeablePermissions() {
   return file.getChangeablePermissions();
 }
コード例 #27
0
 public int hashCode() {
   return file.hashCode();
 }
コード例 #28
0
 @Override
 public String getPermissionsString() {
   return file.getPermissionsString();
 }
コード例 #29
0
 @Override
 public boolean equalsCanonical(Object f) {
   return file.equalsCanonical(f);
 }
コード例 #30
0
 @Override
 public void changePermissions(int permissions) throws IOException {
   file.changePermissions(permissions);
 }