Ad

GetEncryptedData(String) Invokes Inefficient New String(String) Constructor - Findbugs

- 1 answer

I'm trying to convert byte[] to String.and its working fine. But FindBugs is pointing me a Minor issue in my code snippet.

code snippet:

        //Encrypt the data withe public key.
        Cipher cipher = Cipher.getInstance(TRASFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        encryptedData = new String(Base64.encodeToString(encryptedBytes, Base64.DEFAULT));

Findbug Report:

getEncryptedData(String) invokes inefficient new String(String) constructor

In which line i'm getting this Error?

encryptedData = new String(Base64.encodeToString(encryptedBytes, Base64.DEFAULT));

Could some one please brief me what is this exactly? And how can we solve this?

Ad

Answer

Replace

encryptedData = new String(Base64.encodeToString(encryptedBytes, Base64.DEFAULT));

with

encryptedData = Base64.encodeToString(encryptedBytes, Base64.DEFAULT);

Encode to String is already returning the string.

Ad
source: stackoverflow.com
Ad