Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to check base64 in java

import java.util.Base64;

    public static String encodeBase64(String s) {
        return Base64.getEncoder().encodeToString(s.getBytes());
    }

    public static String decodeBase64(String s) {
        try {
            if (isBase64(s)) {
                return new String(Base64.getDecoder().decode(s));
            } else {
                return s;
            }
        } catch (Exception e) {
            return s;
        }
    }

    public static boolean isBase64(String s) {
        String pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(s);

        return m.find();
    }
Comment

PREVIOUS NEXT
Code Example
Java :: java string to number 
Java :: java get length of a string 
Java :: matrice java 
Java :: fragment to fragment 
Java :: how to find some of digits in java 
Java :: java system.out.println 
Java :: creating a directory using java 
Java :: java run code at interval 
Java :: java coalesce 
Java :: declare class constant array java 
Java :: replace regex group() java 
Java :: java class name to string 
Java :: android studio edittext enter pressed 
Java :: arraylist .set java 
Java :: java 2d array length 
Java :: space seperator in string in java 
Java :: calling activity method from fragment: 
Java :: get element in arraylist java 
Java :: calculate time java 
Java :: how to use user uid in android 
Java :: java instanceof keyword 
Java :: cast java 
Java :: java decompiler 
Java :: Changing background color of selected item in recyclerview 
Java :: arraylist to string 
Java :: generate uuid from string 
Java :: mac java version terminal 
Java :: basic java programs 
Java :: button change text java 
Java :: hashmaps java 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =