⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 virtualdirectory.java

📁 一个利用Java语言实现的ftp程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package io;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.Comparator;
import java.util.Date;
import java.util.StringTokenizer;

import util.DateUtils;


/**
 * This class is responsible to handle all virtual directory activities.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class VirtualDirectory implements Serializable {
    
    private final static String NEWLINE  = "\r\n";
    private final static String DELIM    = " ";
    
    private String mstRoot        = "/";
    private String mstCurrDir     = "/";
    
    private boolean mbWritePerm   = false;
    
    
    /**
     * Default constructor does nothing
     */
    public VirtualDirectory() {
    }
        
    /**
     * Set write permission.
     */
    public void setWritePermission(boolean perm) {
        mbWritePerm = perm;
    }
    
    /**
     * Set root directory. Root directory string will always
     * end with '/'.
     */
    public void setRootDirectory(File root) {

       if(root == null) {
           root = new File("/");
       }
       mstRoot = normalizeSeparateChar(root.getAbsolutePath());
        
       // if not ends with '/' - add one
       if(mstRoot.charAt(mstRoot.length()-1) != '/') {
           mstRoot = mstRoot + '/';
       }
       mstCurrDir = "/";
    }
    
    /**
     * Set root directory.
     */
    public void setRootDirectory(String root) throws IOException {
       mstRoot = normalizeSeparateChar(root);
        
       // if not ends with '/' - add one
       if(mstRoot.charAt(mstRoot.length()-1) != '/') {
           mstRoot = mstRoot + '/';
       }
       mstCurrDir = "/";
    }
    
    /**
     * Get write permission in this system
     */
    public boolean getWritePermission() {
        return mbWritePerm;
    }
        
    /**
     * Get current working directory.
     */
    public String getCurrentDirectory() {
        return mstCurrDir;
    }
    
    
    /**
     * Get root directory.
     */
    public String getRootDirectory() {
        return mstRoot;
    }
    
    
    /**
     * Get physical name (wrt the machine root).
     */
    public String getPhysicalName(String virtualName) {
        virtualName = normalizeSeparateChar(virtualName);
        return replaceDots(virtualName);
    }
    
    
    /**
     * Get virtual name (wrt the virtual root). 
     * The return value will never end with '/' unless it is '/'. 
     */
    public String getAbsoluteName(String virtualName) {
        virtualName = normalizeSeparateChar(virtualName);
        String physicalName = replaceDots(virtualName);
        
        String absoluteName = physicalName.substring(mstRoot.length()-1).trim();
        return removeLastSlash(absoluteName);
    }
    
    
    /**
     * Get virtual name (wrt the virtual root). The virtual
     * name will never end with '/' unless it is '/'. 
     */
    public String getVirtualName(String physicalName) {
        physicalName = normalizeSeparateChar(physicalName);
        if (!physicalName.startsWith(mstRoot)) {
            return null;
        }
        
        String virtualName = physicalName.substring(mstRoot.length()-1).trim();
        return removeLastSlash(virtualName);
    }


    /**
     * Change directory. The current directory will never have '/'
     * at the end unless it is '/'.
     * @param dirName change the current working directory.
     * @return true if success
     */
    public boolean changeDirectory(String virtualDir) {

        String physcialDir = getPhysicalName(virtualDir);
        if (physcialDir.equals("")) {
            physcialDir = "/";
        }
        
        File dirFl = new File(physcialDir);
        if (dirFl.exists() && dirFl.isDirectory()) {
            mstCurrDir = physcialDir.substring(mstRoot.length() - 1).trim();
            mstCurrDir = removeLastSlash(mstCurrDir);
            return true;
        }
        
        return false;
    }

    
    /**
     * Check read permission.
     */
    public boolean hasReadPermission(String fileName, boolean bPhysical) {
        if(bPhysical) {
            fileName = normalizeSeparateChar(fileName);
        }
        else {
            fileName = getPhysicalName(fileName);
        }

        if(!fileName.startsWith(mstRoot)) {
            return false;
        }

        return new File(fileName).canRead();
    }
    
    
    /**
     * Chech file write/delete permission.
     */
    public boolean hasWritePermission(String fileName, boolean bPhysical) {

        // no write permission
        if(!mbWritePerm) {
            return false;
        }
        
        // if virtual name - get the physical name
        if(bPhysical) {
            fileName = normalizeSeparateChar(fileName);
        }
        else {
            fileName = getPhysicalName(fileName);
        }

        if(!fileName.startsWith(mstRoot)) {
            return false;
        }

        return new File(fileName).canWrite();
    }


    /**
     * Check file create permission.
     */
    public boolean hasCreatePermission(String fileName, boolean bPhysical) {
        
        // no write permission
        if(!mbWritePerm) {
            return false;
        }
        
        // if virtual name - get the physical name
        if(bPhysical) {
            fileName = normalizeSeparateChar(fileName);
        }
        else {
            fileName = getPhysicalName(fileName);
        }
        
        return fileName.startsWith(mstRoot);
    }
    
    
    /**
     * Print file list. Detail listing.
     * <pre>
     *   -a : display all (including hidden files)
     * </pre>
     * @return true if success
     */
    public boolean printList(String argument, Writer out) throws IOException {
        
        FileLister lister = new FileLister(argument);
        File[] flLst = lister.getFiles();
        if (flLst == null) {
            return false;
        }
        else {
            for(int i=0; i<flLst.length; i++) {
                if ( (!lister.isAll()) && flLst[i].isHidden() ) {
                    continue;
                }
                printLine(flLst[i], out);
                out.write(NEWLINE);
            }
            return true;
        }
    }
    
    
    /**
     * Print file list.
     * <pre>
     *   -l : detail listing
     *   -a : display all (including hidden files)
     * </pre>
     * @return true if success
     */
    public boolean printNList(String argument, Writer out) throws IOException {
        
        FileLister lister = new FileLister(argument);        
        File[] flLst = lister.getFiles();
        if (flLst == null) {
            return false;
        }
        else {
            for(int i=0; i<flLst.length; i++) {
                if ( (!lister.isAll()) && flLst[i].isHidden() ) {
                    continue;
                }
                if(lister.isDetail()) {
                    printLine(flLst[i], out);
                }
                else {
                    out.write(getName(flLst[i]));
                }
                out.write(NEWLINE);
            }
            return true;
        }
    }
    
    /**
     * Get file owner.
     */
    private String getOwner(File fl) {
        return "user";
    }
    
    
    /**
     * Get group name
     */
    private String getGroup(File fl) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -