Java XML Base64 Encode Decode encodeBase64(byte[] bytes)

Here you can find the source of encodeBase64(byte[] bytes)

Description

Convert a byte array into a Base64-encoded String.

License

Open Source License

Parameter

Parameter Description
bytes Data bytes

Return

Base64-encoded String

Declaration

private static String encodeBase64(byte[] bytes) 

Method Source Code

//package com.java2s;
/**************************************************************************
 OmegaT - Computer Assisted Translation (CAT) tool 
  with fuzzy matching, translation memory, keyword search, 
  glossaries, and translation leveraging into updated projects.
    /*  w w  w .jav a2 s  . c  o m*/
 Copyright (C) 2000-2006 Keith Godfrey and Maxym Mykhalchuk
       2007 Didier Briel and Tiago Saboga
       2007 Zoltan Bartko - bartkozoltan@bartkozoltan.com
       2008 Andrzej Sawula
       2010-2013 Alex Buloichik
       2015 Zoltan Bartko, Aaron Madlon-Kay
       2016 Aaron Madlon-Kay
       Home page: http://www.omegat.org/
       Support center: http://groups.yahoo.com/group/OmegaT/
    
 This file is part of OmegaT.
    
 OmegaT is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 OmegaT is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import java.util.Arrays;

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**
     * Convert a byte array into a Base64-encoded String. Convenience method for
     * {@link DatatypeConverter#printBase64Binary(byte[])} (available since Java
     * 1.6) because it's so well hidden.
     * 
     * @param bytes
     *            Data bytes
     * @return Base64-encoded String
     */
    private static String encodeBase64(byte[] bytes) {
        return DatatypeConverter.printBase64Binary(bytes);
    }

    /**
     * Convert a string's <code>charset</code> bytes into a Base64-encoded String.
     * 
     * @param string
     *            a string
     * @param charset
     *            the charset with which to obtain the bytes
     * @return Base64-encoded String
     */
    public static String encodeBase64(String string, Charset charset) {
        return encodeBase64(string.getBytes(charset));
    }

    /**
     * Convert a char array's <code>charset</code> bytes into a Base64-encoded String.
     * Useful for handling passwords. Intermediate buffers are cleared after use.
     * 
     * @param chars
     *            a char array
     * @param charset
     *            the charset with which to obtain the bytes
     * @return Base64-encoded String
     */
    public static String encodeBase64(char[] chars, Charset charset) {
        CharBuffer charBuf = CharBuffer.wrap(chars);
        ByteBuffer byteBuf = charset.encode(charBuf);
        String result = encodeBase64(byteBuf.array());
        Arrays.fill(charBuf.array(), '\0');
        Arrays.fill(byteBuf.array(), (byte) 0);
        return result;
    }
}

Related

  1. decodeBase64(String base64)
  2. decodeBase64(String s)
  3. decodeBase64(String value)
  4. decompressFromBase64(String message)
  5. encodeBase64(byte[] b)
  6. encodeBASE64(byte[] data)
  7. encodeBase64(String input)
  8. encodeBase64(String stringValue)
  9. encodeBase64Binary(byte[] data)