Here i have a code which accept a file content in byte array, i want to check whether its in base64 format or not,before converting it to base64 and returning.. can anyone help me out here
import sun.misc.BASE64Encoder;
public static String encodeInByteArray(byte[] b)
{
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b);
}
Below is the code which i tried to check for base64 format:
import sun.misc.BASE64Encoder;
import java.util.regex.Pattern;
public class Encoder
{
public static String encodeInByteArray(byte[] b)
{
String regex =
"([A-Za-z0-9+/]{4})*"+
"([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)";
Pattern patron = Pattern.compile(regex);
String s=b.toString();
if (!patron.matcher(s).matches()){
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b);
}
else
return s;
}
public static void main(String [] args) throws FileNotFoundException
{
FileInputStream fs= new FileInputStream("Sample.pdf");
String s= fs.toString();
byte[] b = s.getBytes();
encodeInByteArray(b);
}
}