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

📄 smbfile.java

📁 实现网上邻居需要的jar库;可以使用库中的接口实现文件共享的功能
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                case NtStatus.NT_STATUS_OBJECT_NAME_INVALID:
                case NtStatus.NT_STATUS_OBJECT_NAME_NOT_FOUND:
                case NtStatus.NT_STATUS_OBJECT_PATH_NOT_FOUND:
                    break;
                default:
                    throw se;
            }
        }

        attrExpiration = System.currentTimeMillis() + attrExpirationPeriod;

        return isExists;
    }

/**
 * Tests to see if the file this <code>SmbFile</code> represents can be
 * read. Because any file, directory, or other resource can be read if it
 * exists, this method simply calls the <code>exists</code> method.
 *
 * @return <code>true</code> if the file is read-only
 */

    public boolean canRead() throws SmbException {
        if( getType() == TYPE_NAMED_PIPE ) { // try opening the pipe for reading?
            return true;
        }
        return exists(); // try opening and catch sharing violation?
    }

/**
 * Tests to see if the file this <code>SmbFile</code> represents
 * exists and is not marked read-only. By default, resources are
 * considered to be read-only and therefore for <code>smb://</code>,
 * <code>smb://workgroup/</code>, and <code>smb://server/</code> resources
 * will be read-only.
 *
 * @return  <code>true</code> if the resource exists is not marked
 *          read-only
 */

    public boolean canWrite() throws SmbException {
        if( getType() == TYPE_NAMED_PIPE ) { // try opening the pipe for writing?
            return true;
        }
        return exists() && ( attributes & ATTR_READONLY ) == 0;
    }

/**
 * Tests to see if the file this <code>SmbFile</code> represents is a directory.
 *
 * @return <code>true</code> if this <code>SmbFile</code> is a directory
 */

    public boolean isDirectory() throws SmbException {
        if( getUncPath0().length() == 1 ) {
            return true;
        }
        if (!exists()) return false;
        return ( attributes & ATTR_DIRECTORY ) == ATTR_DIRECTORY;
    }

/**
 * Tests to see if the file this <code>SmbFile</code> represents is not a directory.
 *
 * @return <code>true</code> if this <code>SmbFile</code> is not a directory
 */

    public boolean isFile() throws SmbException {
        if( getUncPath0().length() == 1 ) {
            return false;
        }
        exists();
        return ( attributes & ATTR_DIRECTORY ) == 0;
    }

/**
 * Tests to see if the file this SmbFile represents is marked as
 * hidden. This method will also return true for shares with names that
 * end with '$' such as <code>IPC$</code> or <code>C$</code>.
 *
 * @return <code>true</code> if the <code>SmbFile</code> is marked as being hidden
 */

    public boolean isHidden() throws SmbException {
        if( share == null ) {
            return false;
        } else if( getUncPath0().length() == 1 ) {
            if( share.endsWith( "$" )) {
                return true;
            }
            return false;
        }
        exists();
        return ( attributes & ATTR_HIDDEN ) == ATTR_HIDDEN;
    }

/**
 * If the path of this <code>SmbFile</code> falls within a DFS volume,
 * this method will return the referral path to which it maps. Otherwise
 * <code>null</code> is returned.
 */

    public String getDfsPath() throws SmbException {
        resolveDfs(null);
        if( dfsReferral == null ) {
            return null;
        }
        String path = "smb:/" + dfsReferral.server + "/" + dfsReferral.share + unc;
        path = path.replace( '\\', '/' );
        if (isDirectory()) {
            path += '/';
        }
        return path;
    }

/**
 * Retrieve the time this <code>SmbFile</code> was created. The value
 * returned is suitable for constructing a {@link java.util.Date} object
 * (i.e. seconds since Epoch 1970). Times should be the same as those
 * reported using the properties dialog of the Windows Explorer program.
 *
 * For Win95/98/Me this is actually the last write time. It is currently
 * not possible to retrieve the create time from files on these systems.
 *
 * @return The number of milliseconds since the 00:00:00 GMT, January 1,
 *         1970 as a <code>long</code> value
 */
    public long createTime() throws SmbException {
        if( getUncPath0().length() > 1 ) {
            exists();
            return createTime;
        }
        return 0L;
    }
/**
 * Retrieve the last time the file represented by this
 * <code>SmbFile</code> was modified. The value returned is suitable for
 * constructing a {@link java.util.Date} object (i.e. seconds since Epoch
 * 1970). Times should be the same as those reported using the properties
 * dialog of the Windows Explorer program.
 *
 * @return The number of milliseconds since the 00:00:00 GMT, January 1,
 *         1970 as a <code>long</code> value
 */
    public long lastModified() throws SmbException {
        if( getUncPath0().length() > 1 ) {
            exists();
            return lastModified;
        }
        return 0L;
    }
/**
 * List the contents of this SMB resource. The list returned by this
 * method will be;
 *
 * <ul>
 * <li> files and directories contained within this resource if the
 * resource is a normal disk file directory,
 * <li> all available NetBIOS workgroups or domains if this resource is
 * the top level URL <code>smb://</code>,
 * <li> all servers registered as members of a NetBIOS workgroup if this
 * resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
 * <li> all browseable shares of a server including printers, IPC
 * services, or disk volumes if this resource is a server URL in the form
 * <code>smb://server/</code>,
 * <li> or <code>null</code> if the resource cannot be resolved.
 * </ul>
 *
 * @return A <code>String[]</code> array of files and directories,
 * workgroups, servers, or shares depending on the context of the
 * resource URL
 */
    public String[] list() throws SmbException {
        return list( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, null );
    }

/**
 * List the contents of this SMB resource. The list returned will be
 * identical to the list returned by the parameterless <code>list()</code>
 * method minus filenames filtered by the specified filter.
 *
 * @param filter a filename filter to exclude filenames from the results
 * @throws SmbException
 # @return An array of filenames
 */
    public String[] list( SmbFilenameFilter filter ) throws SmbException {
        return list( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, filter, null );
    }

/**
 * List the contents of this SMB resource as an array of
 * <code>SmbFile</code> objects. This method is much more efficient than
 * the regular <code>list</code> method when querying attributes of each
 * file in the result set.
 * <p>
 * The list of <code>SmbFile</code>s returned by this method will be;
 *
 * <ul>
 * <li> files and directories contained within this resource if the
 * resource is a normal disk file directory,
 * <li> all available NetBIOS workgroups or domains if this resource is
 * the top level URL <code>smb://</code>,
 * <li> all servers registered as members of a NetBIOS workgroup if this
 * resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
 * <li> all browseable shares of a server including printers, IPC
 * services, or disk volumes if this resource is a server URL in the form
 * <code>smb://server/</code>,
 * <li> or <code>null</code> if the resource cannot be resolved.
 * </ul>
 *
 * @return An array of <code>SmbFile</code> objects representing file
 * and directories, workgroups, servers, or shares depending on the context
 * of the resource URL
 */
    public SmbFile[] listFiles() throws SmbException {
        return listFiles( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, null );
    }

/**
 * The CIFS protocol provides for DOS "wildcards" to be used as
 * a performance enhancement. The client does not have to filter
 * the names and the server does not have to return all directory
 * entries.
 * <p>
 * The wildcard expression may consist of two special meta
 * characters in addition to the normal filename characters. The '*'
 * character matches any number of characters in part of a name. If
 * the expression begins with one or more '?'s then exactly that
 * many characters will be matched whereas if it ends with '?'s
 * it will match that many characters <i>or less</i>.
 * <p>
 * Wildcard expressions will not filter workgroup names or server names.
 *
 * <blockquote><pre>
 * winnt> ls c?o*
 * clock.avi                  -rw--      82944 Mon Oct 14 1996 1:38 AM
 * Cookies                    drw--          0 Fri Nov 13 1998 9:42 PM
 * 2 items in 5ms
 * </pre></blockquote>
 *
 * @param wildcard a wildcard expression
 * @throws SmbException
 * @return An array of <code>SmbFile</code> objects representing file
 * and directories, workgroups, servers, or shares depending on the context
 * of the resource URL
 */

    public SmbFile[] listFiles( String wildcard ) throws SmbException {
        return listFiles( wildcard, ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, null );
    }
/**
 * List the contents of this SMB resource. The list returned will be
 * identical to the list returned by the parameterless <code>listFiles()</code>
 * method minus files filtered by the specified filename filter.
 *
 * @param filter a filter to exclude files from the results
 * @return An array of <tt>SmbFile</tt> objects
 * @throws SmbException
 */
    public SmbFile[] listFiles( SmbFilenameFilter filter ) throws SmbException {
        return listFiles( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, filter, null );
    }
/**
 * List the contents of this SMB resource. The list returned will be
 * identical to the list returned by the parameterless <code>listFiles()</code>
 * method minus filenames filtered by the specified filter.
 *
 * @param filter a file filter to exclude files from the results
 * @return An array of <tt>SmbFile</tt> objects
 */
    public SmbFile[] listFiles( SmbFileFilter filter ) throws SmbException {
        return listFiles( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, filter );
    }
    String[] list( String wildcard, int searchAttributes,
                SmbFilenameFilter fnf, SmbFileFilter ff ) throws SmbException {
        ArrayList list = new ArrayList();
        doEnum(list, false, wildcard, searchAttributes, fnf, ff);
        return (String[])list.toArray(new String[list.size()]);
    }
    SmbFile[] listFiles( String wildcard, int searchAttributes,
                SmbFilenameFilter fnf, SmbFileFilter ff ) throws SmbException {
        ArrayList list = new ArrayList();
        doEnum(list, true, wildcard, searchAttributes, fnf, ff);
        return (SmbFile[])list.toArray(new SmbFile[list.size()]);
    }
    void doEnum(ArrayList list,
                    boolean files,
                    String wildcard,
                    int searchAttributes,
                    SmbFilenameFilter fnf,
                    SmbFileFilter ff) throws SmbException {
        if (ff != null && ff instanceof DosFileFilter) {
            DosFileFilter dff = (DosFileFilter)ff;
            if (dff.wildcard != null)
                wildcard = dff.wildcard;
            searchAttributes = dff.attributes;
        }

        try {
            int hostlen = url.getHost().length();
            if (hostlen == 0 || getType() == TYPE_WORKGROUP) {
                doNetServerEnum(list, files, wildcard, searchAttributes, fnf, ff);
            } else if (share == null) {
                doShareEnum(list, files, wildcard, searchAttributes, fnf, ff);
            } else {
                doFindFirstNext(list, files, wildcard, searchAttributes, fnf, ff);
            }
        } catch (UnknownHostException uhe) {
            throw new SmbException(url.toString(), uhe);
        } catch (MalformedURLException mue) {
            throw new SmbException(url.toString(), mue);
        }
    }
    void doShareEnum(ArrayList list,
                boolean files,
                String wildcard,
                int searchAttributes,
                SmbFilenameFilter fnf,
                SmbFileFilter ff) throws SmbException,
                                UnknownHostException,
                                MalformedURLException {
        String p = url.getPath();
        IOException last = null;
        FileEntry[] entries;
        UniAddress addr;
        FileEntry e;
        HashMap map;

        if (p.lastIndexOf('/') != (p.length() - 1))
            throw new SmbException(url.toString() + " directory must end with '/'");
        if (getType() != TYPE_SERVER)
            throw new SmbException("The requested list operations is invalid: " + url.toString());

        map = new HashMap();

        if (dfs.isTrustedDomain(getServer(), auth)) {
            /* The server name is actually the name of a trusted
             * domain. Add DFS roots to the list.
             */
            try {
                entries = doDfsRootEnum();
                for (int ei = 0; ei < entries.length; ei++) {
                 

⌨️ 快捷键说明

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