1

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);
  }
}
sam
  • 101
  • 2
  • 13

3 Answers3

2

Calling b.toString() doesn't do what you might expect - the resulting string will be something like [B@106d69c, because arrays don't override toString. (In a similar vein, calling fs.toString() won't give you the contents of the file as a string).

To get a String from a byte[], use the constructor:

new String(b)

But you probably want to specify a particular charset, e.g.:

new String(b, StandardCharsets.ISO_8859_1)

otherwise you may get different results, depending upon your JVM's configuration.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

First solution you could parse the file, or parse the file part way (to save resources) and determine if a line is base64 encoded. See this answer for the String base64 encoding check.

How to check whether the string is base64 encoded or not

A second solution would be is that if you have complete control over the file saving and encoding, you could place a byte at the head or tail of the file indicated if its base64 encoded or not, which should be faster then the above solution.

Community
  • 1
  • 1
Mr00Anderson
  • 854
  • 8
  • 16
0

You can use Base64.isBase64(byte[] arrayOctet) from apache's commons-codec.

Be aware that whitespaces are valid at the moment as stated in the documentation.

Y.E.
  • 902
  • 7
  • 14