Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid warning for the Base 64?

Tags:

java

I have just tried a sample code form net it shows a warning as follows

SimpleConvertImage.java:7: warning:com.sun.org.apache.xerces.internal.impl.dv.util.Base64 is internal proprietary API and may be removed in a future release import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; ^

SimpleConvertImage.java:16: warning: com.sun.org.apache.xerces.internal.impl.dv.util. Base64 is internal proprietary API and may be removed in a future release String base64String=Base64.encode(baos.toByteArray()); ^

SimpleConvertImage.java:19: warning: com.sun.org.apache.xerces.internal.impl.dv.util .Base64 is internal proprietary API and may be removed in a future release byte[] bytearray =Base64.decode(base64String); ^

the code is the below one

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; 
public class SimpleConvertImage {
    public static void main(String[] args) throws IOException{
        String dirName="/root/Desktop";
        ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
        BufferedImage img=ImageIO.read(new File(dirName,"Screenshot.png"));
        ImageIO.write(img, "png", baos);
        baos.flush();

        String base64String=Base64.encode(baos.toByteArray());
        baos.close();

        byte[] bytearray =Base64.decode(base64String);

        BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
        ImageIO.write(imag, "png", new File(dirName,"snap3.png"));
    }
}

like image 934
ambrosia1993 Avatar asked Feb 20 '14 10:02

ambrosia1993


People also ask

How secure is Base64?

Base64 is not an encryption, it is an encoding. It's role is to make sure the password can be stored in the database nicely and special characters aren't a problem. It does nothing to protect the password. From security standpoint, it is exactly the same as storing it without any encoding.

Can Base64 be decoded?

Base64 Decode is very unique tool to decode base64 data to plain text. This tool saves your time and helps to decode base64 data. This tool allows loading the Base64 data URL, which loads base64 encoded text and decodes to human readable text.

How do I Decript Base64?

Decode from Base64 format. Simply enter your data then push the decode button. For encoded binaries (like images, documents, etc.) use the file upload form a little further down on this page. Source character set.

What is == in Base64?

When decoding Base64 text, four characters are typically converted back to three bytes. The only exceptions are when padding characters exist. A single = indicates that the four characters will decode to only two bytes, while == indicates that the four characters will decode to only a single byte.


2 Answers

Don't use internal com.sun.* packages. If you are on v6 or greater you can use DatatypeConverter. Your code would look like:

String base64String = DatatypeConverter.printBase64Binary(baos.toByteArray());
byte[] bytearray = DatatypeConverter.parseBase64Binary(base64String);
like image 121
mikea Avatar answered Oct 13 '22 03:10

mikea


You can also use: java.util.Base64 Added to Java 1.8

String encryptedValue = new String(Base64.getEncoder().encode(bytesToEncode));
byte[] decodedValue = Base64.getDecoder().decode(encryptedDataString);
like image 30
Mayank Avatar answered Oct 13 '22 03:10

Mayank