Java Base64 Encode encodeBase64(String data)

Here you can find the source of encodeBase64(String data)

Description

Encodes a String as a base64 String.

License

Open Source License

Parameter

Parameter Description
data a String to encode.

Return

a base64 encoded String.

Declaration

public static String encodeBase64(String data) 

Method Source Code


//package com.java2s;
/*//from   w w  w  .j  ava 2 s .co  m
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Encodes a String as a base64 String.
     *
     * @param data a String to encode.
     * @return a base64 encoded String.
     */
    public static String encodeBase64(String data) {
        byte[] bytes = null;
        try {
            bytes = data.getBytes("UTF-8");
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        }
        return encodeBase64(bytes);
    }

    /**
     * Encodes a byte array into a base64 String.
     *
     * @param data a byte array to encode.
     * @return a base64 encode String.
     */
    public static String encodeBase64(byte[] data) {
        // Encode the String. We pass in a flag to specify that line
        // breaks not be added. This is consistent with our previous base64
        // implementation. Section 2.1 of 3548 (base64 spec) also specifies
        // no line breaks by default.        
        return (java.util.Base64.getEncoder().encodeToString(data));
        //return org.jivesoftware.util.Base64.encodeBytes(data, org.jivesoftware.util.Base64.DONT_BREAK_LINES);
    }
}

Related

  1. base64encoding(byte[] bytes)
  2. base64UrlDecode(final String encodedString)
  3. base64UrlEncode(final byte[] v)
  4. encodeBASE64(InputStream in, OutputStream out)
  5. encodeBase64(InputStream is, OutputStream os)
  6. encodeBase64(String data)
  7. toBase64(byte[] array)
  8. toBase64(byte[] aValue)
  9. toBase64(byte[] buf, int start, int length)