public boolean isValidInet6Address()

in ldap/model/src/main/java/org/apache/directory/api/ldap/model/url/LdapUrl.java [530:643]


    public boolean isValidInet6Address( String inet6Address )
    {
        boolean containsCompressedZeroes = inet6Address.contains( "::" );

        if ( containsCompressedZeroes && ( inet6Address.indexOf( "::" ) != inet6Address.lastIndexOf( "::" ) ) )
        {
            return false;
        }

        if ( ( inet6Address.startsWith( ":" ) && !inet6Address.startsWith( "::" ) )
            || ( inet6Address.endsWith( ":" ) && !inet6Address.endsWith( "::" ) ) )
        {
            return false;
        }

        String[] octets = inet6Address.split( ":" );

        if ( containsCompressedZeroes )
        {
            List<String> octetList = new ArrayList<>( Arrays.asList( octets ) );

            if ( inet6Address.endsWith( "::" ) )
            {
                // String.split() drops ending empty segments
                octetList.add( "" );
            }
            else if ( inet6Address.startsWith( "::" ) && !octetList.isEmpty() )
            {
                octetList.remove( 0 );
            }

            octets = octetList.toArray( new String[octetList.size()] );
        }

        if ( octets.length > 8 )
        {
            return false;
        }

        int validOctets = 0;
        int emptyOctets = 0;

        for ( int index = 0; index < octets.length; index++ )
        {
            String octet = octets[index];

            if ( octet.length() == 0 )
            {
                emptyOctets++;

                if ( emptyOctets > 1 )
                {
                    return false;
                }
            }
            else
            {
                emptyOctets = 0;

                if ( octet.contains( "." ) )
                { // contains is Java 1.5+
                    if ( !inet6Address.endsWith( octet ) )
                    {
                        return false;
                    }

                    if ( index > octets.length - 1 || index > 6 )
                    {
                        // IPV4 occupies last two octets
                        return false;
                    }

                    if ( !isValidInet4Address( octet ) )
                    {
                        return false;
                    }

                    validOctets += 2;

                    continue;
                }

                if ( octet.length() > 4 )
                {
                    return false;
                }

                int octetInt = 0;

                try
                {
                    octetInt = Integer.valueOf( octet, 16 ).intValue();
                }
                catch ( NumberFormatException e )
                {
                    return false;
                }

                if ( octetInt < 0 || octetInt > 0xffff )
                {
                    return false;
                }
            }

            validOctets++;
        }

        if ( validOctets < 8 && !containsCompressedZeroes )
        {
            return false;
        }

        return true;
    }