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

📄 nbtaddress.java

📁 java ftp 操作代码,程序可以直接运行
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

        if( addr == null ) {
            /* This was copied amost verbatim from InetAddress.java. See the
             * comments there for a description of how the LOOKUP_TABLE prevents
             * redundant queries from going out on the wire.
             */
            if(( addr = (NbtAddress)checkLookupTable( name )) == null ) {
                try {
                    addr = CLIENT.getByName( name, svr );
                } catch( UnknownHostException uhe ) {
                    addr = UNKNOWN_ADDRESS;
                } finally {
                    cacheAddress( name, addr );
                    updateLookupTable( name );
                }
            }
        }
        if( addr == UNKNOWN_ADDRESS ) {
            throw new UnknownHostException( name.toString() );
        }
        return addr;
    }

    private static Object checkLookupTable( Name name ) {
        Object obj;

        synchronized( LOOKUP_TABLE ) {
            if( LOOKUP_TABLE.containsKey( name ) == false ) {
                LOOKUP_TABLE.put( name, name );
                return null;
            }
            while( LOOKUP_TABLE.containsKey( name )) {
                try {
                    LOOKUP_TABLE.wait();
                } catch( InterruptedException e ) {
                }
            }
        }
        obj = getCachedAddress( name );
        if( obj == null ) {
            synchronized( LOOKUP_TABLE ) {
                LOOKUP_TABLE.put( name, name );
            }
        }

        return obj;
    }
    private static void updateLookupTable( Name name ) {
        synchronized( LOOKUP_TABLE ) {
            LOOKUP_TABLE.remove( name );
            LOOKUP_TABLE.notifyAll();
        }
    }

/** 
 * Retrieves the local host address.
 *
 * @throws UnknownHostException This is not likely as the IP returned
 *                    by <code>InetAddress</code> should be available
 */ 

    public static NbtAddress getLocalHost() throws UnknownHostException {
        return localhost;
    }
    public static Name getLocalName() {
        return localhost.hostName;
    }

/** 
 * Determines the address of a host given it's host name. The name can be a NetBIOS name like
 * "freto" or an IP address like "192.168.1.15". It cannot be a DNS name;
 * the analygous {@link jcifs.UniAddress} or {@link java.net.InetAddress}
 * <code>getByName</code> methods can be used for that.
 *
 * @param host hostname to resolve
 * @throws java.net.UnknownHostException if there is an error resolving the name
 */

    public static NbtAddress getByName( String host )
                                        throws UnknownHostException {
        return getByName( host, 0x00, null );
    }

/** 
 * Determines the address of a host given it's host name. NetBIOS
 * names also have a <code>type</code>. Types(aka Hex Codes)
 * are used to distiquish the various services on a host. <a
 * href="../../../nbtcodes.html">Here</a> is
 * a fairly complete list of NetBIOS hex codes. Scope is not used but is
 * still functional in other NetBIOS products and so for completeness it has been
 * implemented. A <code>scope</code> of <code>null</code> or <code>""</code>
 * signifies no scope.
 *
 * @param host the name to resolve
 * @param type the hex code of the name
 * @param scope the scope of the name
 * @throws java.net.UnknownHostException if there is an error resolving the name
 */

    public static NbtAddress getByName( String host,
                                        int type,
                                        String scope )
                                        throws UnknownHostException {

        return getByName( host, type, scope, null );
    }

/* 
 * The additional <code>svr</code> parameter specifies the address to
 * query. This might be the address of a specific host, a name server,
 * or a broadcast address.
 */ 

    public static NbtAddress getByName( String host,
                                        int type,
                                        String scope,
                                        InetAddress svr )
                                        throws UnknownHostException {

        if( host == null || host.length() == 0 ) {
            return getLocalHost();
        }
        if( !Character.isDigit( host.charAt(0) )) {
            return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
        } else {
            int IP = 0x00;
            int hitDots = 0;
            char[] data = host.toCharArray();

            for( int i = 0; i < data.length; i++ ) {
                char c = data[i];
                if( c < 48 || c > 57 ) {
                    return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
                }
                int b = 0x00;
                while( c != '.' ) {
                    if( c < 48 || c > 57 ) {
                        return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
                    }
                    b = b * 10 + c - '0';

                    if( ++i >= data.length )
                        break;

                    c = data[i];
                }
                if( b > 0xFF ) {
                    return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
                }
                IP = ( IP << 8 ) + b;
                hitDots++;
            }
            if( hitDots != 4 || host.endsWith( "." )) {
                return (NbtAddress)doNameQuery( new Name( host, type, scope ), svr );
            }
            return new NbtAddress( UNKNOWN_NAME, IP, false, B_NODE );
        }
    }

    public static NbtAddress[] getAllByName( String host,
                                        int type,
                                        String scope,
                                        InetAddress svr )
                                        throws UnknownHostException {
        return CLIENT.getAllByName( new Name( host, type, scope ), svr );
    }

/**
 * Retrieve all addresses of a host by it's address. NetBIOS hosts can
 * have many names for a given IP address. The name and IP address make the
 * NetBIOS address. This provides a way to retrieve the other names for a
 * host with the same IP address.
 *
 * @param host hostname to lookup all addresses for
 * @throws java.net.UnknownHostException if there is an error resolving the name
 */


    public static NbtAddress[] getAllByAddress( String host )
                                                throws UnknownHostException {
        return getAllByAddress( getByName( host, 0x00, null ));
    }


/**
 * Retrieve all addresses of a host by it's address. NetBIOS hosts can
 * have many names for a given IP address. The name and IP address make
 * the NetBIOS address. This provides a way to retrieve the other names
 * for a host with the same IP address.  See {@link #getByName}
 * for a description of <code>type</code>
 * and <code>scope</code>.
 *
 * @param host hostname to lookup all addresses for
 * @param type the hexcode of the name
 * @param scope the scope of the name
 * @throws java.net.UnknownHostException if there is an error resolving the name
 */


    public static NbtAddress[] getAllByAddress( String host,
                                        int type,
                                        String scope )
                                        throws UnknownHostException {
        return getAllByAddress( getByName( host, type, scope ));
    }


/**
 * Retrieve all addresses of a host by it's address. NetBIOS hosts can
 * have many names for a given IP address. The name and IP address make the
 * NetBIOS address. This provides a way to retrieve the other names for a
 * host with the same IP address.
 *
 * @param addr the address to query
 * @throws UnknownHostException if address cannot be resolved
 */

    public static NbtAddress[] getAllByAddress( NbtAddress addr )
                                                throws UnknownHostException {
        try {
            NbtAddress[] addrs = CLIENT.getNodeStatus( addr );
            cacheAddressArray( addrs );
            return addrs;
        } catch( UnknownHostException uhe ) {
            throw new UnknownHostException( "no name with type 0x" +
                            Hexdump.toHexString( addr.hostName.hexCode, 2 ) +
                            ((( addr.hostName.scope == null ) ||
                            ( addr.hostName.scope.length() == 0 )) ?
                            " with no scope" : " with scope " + addr.hostName.scope ) +
                            " for host " + addr.getHostAddress() );
        }
    }

    public static InetAddress getWINSAddress() {
        return NBNS.length == 0 ? null : NBNS[nbnsIndex];
    }
    public static boolean isWINS( InetAddress svr ) {
        for( int i = 0; svr != null && i < NBNS.length; i++ ) {
            if( svr.hashCode() == NBNS[i].hashCode() ) {
                return true;
            }
        }
        return false;
    }
    static InetAddress switchWINS() {
        nbnsIndex = (nbnsIndex + 1) < NBNS.length ? nbnsIndex + 1 : 0;
        return NBNS.length == 0 ? null : NBNS[nbnsIndex];
    }

    Name hostName;
    int address, nodeType;
    boolean groupName,
        isBeingDeleted,
        isInConflict,
        isActive,
        isPermanent,
        isDataFromNodeStatus;
    byte[] macAddress;
    String calledName;

    NbtAddress( Name hostName, int address, boolean groupName, int nodeType ) {
        this.hostName = hostName;
        this.address = address;
        this.groupName = groupName;
        this.nodeType = nodeType;
    }

    NbtAddress( Name hostName,
                int address,
                boolean groupName,
                int nodeType,
                boolean isBeingDeleted,
                boolean isInConflict,
                boolean isActive,
                boolean isPermanent,
                byte[] macAddress ) {

/* The NodeStatusResponse.readNodeNameArray method may also set this
 * information. These two places where node status data is populated should
 * be consistent. Be carefull!
 */
        this.hostName = hostName;
        this.address = address;
        this.groupName = groupName;
        this.nodeType = nodeType;
        this.isBeingDeleted = isBeingDeleted;
        this.isInConflict = isInConflict;
        this.isActive = isActive;
        this.isPermanent = isPermanent;

⌨️ 快捷键说明

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