Created
September 10, 2011 08:37
-
-
Save oluies/1208120 to your computer and use it in GitHub Desktop.
validate base64string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static final Pattern pattern = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"); | |
public static boolean isBase642(String base64) { | |
final Matcher m = pattern.matcher(base64); | |
return m.matches(); | |
} | |
// with guava | |
// this is not really correct as = should only be allowed in the end | |
static final CharMatcher base64Chars = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).or(CharMatcher.WHITESPACE).or(CharMatcher.DIGIT).or(CharMatcher.is('=') ); | |
public static boolean isBase64(String base64) { | |
return base64Chars.matchesAllOf(base64); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment