Analyse du code source URI

Utilisez l'URI pour vérifier l'url, le code suivant:

import org.apache.commons.validator.routines.UrlValidator;

import java.net.URI;
import java.net.URISyntaxException;

public class UrlUtils {
    
    

    public static void main(String[] args) {
    
    
        String url = "http://www.jiaobuchong.com?name=tom&request={\"hobby\":\"film\"}";
        System.out.println(new UrlValidator().isValid(url));
        isValidUrl(url);
    }

    public static boolean isValidUrl(String url) {
    
    
        try {
    
    
            URI uri = new URI(url);
            return true;
        } catch (URISyntaxException e) {
    
    
            e.printStackTrace();
        }
        return false;
    }
}

Si l'url contient {}""sera lancée URISyntaxException, pas par chèque. En regardant le code source dans l'URI, il y a une vérification qui est assez intéressante.

Premièrement, analysez la méthode checkChar

1. Obtenez le binaire de la plage de caractères légaux via highMask

private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
private static final long L_LOWALPHA = 0L;
private static final long L_UPALPHA = 0L;

private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
private static final long H_LOWALPHA = highMask('a', 'z');
private static final long H_UPALPHA = highMask('A', 'Z');
checkChar(0, L_ALPHA, H_ALPHA, "scheme name");

Le résultat de L_ALPHA est égal à 0. Commençons maintenant la partie la plus intéressante, voyons ce que fait highMask:

    // Compute a high-order mask for the characters
    // between first and last, inclusive
    private static long highMask(char first, char last) {
    
    
        long m = 0;
        // Math.min 表示从 ASCII 的范围里取值
        // Math.max(Math.min(first, 127), 64) 表示在 ASCII 码表[64, 127]之间的字符
        // 减去 64 表示不包括 64 这个字符,相对 64 之间还有多少个个字符
        int f = Math.max(Math.min(first, 127), 64) - 64;
        int l = Math.max(Math.min(last, 127), 64) - 64;
        for (int i = f; i <= l; i++)
            m |= 1L << i;
        return m;
    }

highMask ( 'a', 'z ') m binaire:
Insérez la description de l'image ici
highMask ( 'A', 'Z') m binaire:
Insérez la description de l'image ici
H_ALPHA = H_LOWALPHA | H_UPALPHA étape est le résultat:
11111111111111111111111111000000111111111111111111111111110Les résultats après la phase représentée H_UPALPHA a-zA-Zcette gamme Caractères, un radis a un noyau, le bit de noyau correspondant aux données hors de cette plage est 0.

2. Méthode de correspondance

   // Tell whether the given character is permitted by the given mask pair
    private static boolean match(char c, long lowMask, long highMask) {
    
    
        if (c == 0) // 0 doesn't have a slot in the mask. So, it never matches.
            return false;
        if (c < 64)
            return ((1L << c) & lowMask) != 0;
        if (c < 128)
            // 左移 (c - 64) 位,和 highMask 进行 & 运算,如果不等于 0 就表示这个字符是合法的
            return ((1L << (c - 64)) & highMask) != 0;
        return false;
    }

Deuxièmement, analysez la méthode checkChars

Grâce à l'analyse ci-dessus, le principe de l'URI qui détermine si un caractère légal est: convertir le caractère dans la plage légale en un nombre binaire, puis effectuer l'opération ET entre le caractère entrant et le binaire. S'il n'est pas égal à 0, cela signifie que le caractère est légal. .

Analysons à nouveau cette méthode:

checkChars(1, p, L_SCHEME, H_SCHEME, "scheme name");

Grâce aux calculs dans le code, le résultat de L_SCHEME est:

0 | lowMask('0', '9') | lowMask("+-.")

H_SCHEME:

H_SCHEME = highMask('a', 'z') | highMask('A', 'Z') | 0 | highMask("+-.")

En regardant le code de lowMask, le résultat généré m représente la plage de [0-9] binaire:

    // Compute a low-order mask for the characters
    // between first and last, inclusive
    private static long lowMask(char first, char last) {
    
    
        long m = 0;
        int f = Math.max(Math.min(first, 63), 0);
        int l = Math.max(Math.min(last, 63), 0);
        for (int i = f; i <= l; i++)
            m |= 1L << i;
        return m;
    }

Puis dans la méthode de match:

    // Tell whether the given character is permitted by the given mask pair
    private static boolean match(char c, long lowMask, long highMask) {
    
    
        if (c == 0) // 0 doesn't have a slot in the mask. So, it never matches.
            return false;
        if (c < 64)
            // 对于小于 64 的字符,和 lowMask 进行与运算,不等于0表示合法的字符
            return ((1L << c) & lowMask) != 0;
        if (c < 128)
            return ((1L << (c - 64)) & highMask) != 0;
        return false;
    }

Je suppose que tu aimes

Origine blog.csdn.net/jiaobuchong/article/details/102757459
conseillé
Classement