How to check that byte array is Base64 encoded?

How to check that byte array is Base64 encoded?

I have an array of bytes, which I need to test if it is base64 encoded.


You can use apache's Base64.isBase64(byte[]) method:

import org.apache.commons.codec.binary.Base64;

...

boolean isBase64 = Base64.isBase64(bytes);

Base64-encoded strings have length divisible by 4, and consist of characters A-Z, a-z, 0-9, +, and /. If your array of bytes has length that is not divisible by 4, or contains bytes that map to characters outside these ranges, then it does not represent a Base64-encoded string; otherwise, your array of bytes represents a valid Base-64 encoded string.