Q:

How to check blob input string is hex in java?

In our product, in a java class we are receiving Blob in string format and we need to check whether this incoming blob is a hex string (Blob can be of any size) and based on the boolean result we will decide further Blob processing(because in our case MS-SQL is returning Hex and MySQL,Oracle are returning pure Blob). Is it good practice to use Pattern, Matcher and matches() to check whether this incoming Blob is hex string ? 
Following is my code snippet : 
public static boolean isHexadecimal(String blobdata) {

    Pattern p = Pattern.compile("[0-9a-fA-F]+");
    Matcher m = p.matcher(blobdata);

    if (m.matches()) {
        return true;
    }
    return false;
}

A:

The regex sports a deficiency insofar as it only flags that your blobdata contains a substring in hexadecimal ( not even taking into account that the number of consecutive hex bytes must be even if it represents an octet sequence ). Use start/end anchors to rectify that:
Pattern p = Pattern.compile("^[0-9a-fA-F]+$");

An alternative might be to invert the matching ruling out the occurrence of non-hex data in your blob. Of course, this is only viable if you are indeed after hex-only content:
public static boolean isHexadecimal(String blobdata) {

    Pattern p = Pattern.compile("[^0-9a-fA-F]");
    Matcher m = p.matcher(blobdata);

    if (m.matches()) {
        return false;
    }
    return true;
}

