private boolean netscapeDomainMatches(final String domain, final String host) {
        if (domain == null || host == null) {
            return false;
        }

        // if there's no embedded dot in domain and domain is not .local
        boolean isLocalDomain = ".local".equalsIgnoreCase(domain);
        int embeddedDotInDomain = domain.indexOf('.');
        if (embeddedDotInDomain == 0) {
            embeddedDotInDomain = domain.indexOf('.', 1);
        }
        if (!isLocalDomain && (embeddedDotInDomain == -1 || embeddedDotInDomain == domain.length() - 1)) {
            return false;
        }

        // if the host name contains no dot and the domain name is .local
        int firstDotInHost = host.indexOf('.');
        if (firstDotInHost == -1 && isLocalDomain) {
            return true;
        }

        int domainLength = domain.length();
        int lengthDiff = host.length() - domainLength;
        if (lengthDiff == 0) {
            // if the host name and the domain name are just string-compare euqal
            return host.equalsIgnoreCase(domain);
        }
        else if (lengthDiff > 0) {
            // need to check H & D component
            String H = host.substring(0, lengthDiff);
            String D = host.substring(lengthDiff);

            return (D.equalsIgnoreCase(domain));
        }
        else if (lengthDiff == -1) {
            // if domain is actually .host
            return (domain.charAt(0) == '.' &&
                    host.equalsIgnoreCase(domain.substring(1)));
        }

        return false;
    }
private boolean netscapeDomainMatches ( final String domain , final String host ) { if ( domain == null || host == null ) { return false ; } boolean isLocalDomain = ".local" . equalsIgnoreCase ( domain ) ; int embeddedDotInDomain = domain . indexOf ( '.' ) ; if ( embeddedDotInDomain == 0 ) { embeddedDotInDomain = domain . indexOf ( '.' , 1 ) ; } if ( ! isLocalDomain && ( embeddedDotInDomain == - 1 || embeddedDotInDomain == domain . length ( ) - 1 ) ) { return false ; } int firstDotInHost = host . indexOf ( '.' ) ; if ( firstDotInHost == - 1 && isLocalDomain ) { return true ; } int domainLength = domain . length ( ) ; int lengthDiff = host . length ( ) - domainLength ; if ( lengthDiff == 0 ) { return host . equalsIgnoreCase ( domain ) ; } else if ( lengthDiff > 0 ) { String H = host . substring ( 0 , lengthDiff ) ; String D = host . substring ( lengthDiff ) ; return ( D . equalsIgnoreCase ( domain ) ) ; } else if ( lengthDiff == - 1 ) { return ( domain . charAt ( 0 ) == '.' && host . equalsIgnoreCase ( domain . substring ( 1 ) ) ) ; } return false ; }
shamelessly copied from jdk8 source code for InMemoryCookieStore