com.github.adanac.framework.sso.common.util.Base64Util.java Source code

Java tutorial

Introduction

Here is the source code for com.github.adanac.framework.sso.common.util.Base64Util.java

Source

/**
 * Copyright (c) 2011-2014, adanac (1320643091@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.adanac.framework.sso.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.UrlBase64;

import com.github.adanac.framework.sso.SSOConfig;

/**
 * <p>
 * BASE64??
 * </p>
 * <p>
 * ?bcprov-jdk14-1.48.jar
 * </p>
 * <p>
 * @author   adanac
 * @Date    2014-6-17
 */
public class Base64Util {

    /**
     * ??
     */
    private static final int CACHE_SIZE = 1024;

    /**
     * <p>
     * BASE64??
     * </p>
     * 
     * @param base64
     * @return
     * @throws Exception
     */
    public static byte[] decode(String base64) throws Exception {
        return Base64.decode(base64.getBytes());
    }

    /**
     * <p>
     * ??BASE64
     * </p>
     * 
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(byte[] bytes) throws Exception {
        return new String(Base64.encode(bytes));
    }

    /**
     * BASE64 encrypt
     * 
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] key) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        byte[] b = UrlBase64.encode(key);
        return new String(b, SSOConfig.getEncoding());
    }

    /**
     * BASE64 decrypt
     * 
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        return UrlBase64.decode(key.getBytes(SSOConfig.getEncoding()));
    }

    /**
     * <p>
     * ?BASE64
     * </p>
     * <p>
     * ?
     * </p>
     * 
     * @param filePath ?
     * @return
     * @throws Exception
     */
    public static String encodeFile(String filePath) throws Exception {
        byte[] bytes = fileToByte(filePath);
        return encode(bytes);
    }

    /**
     * <p>
     * BASE64
     * </p>
     * 
     * @param filePath ?
     * @param base64 ?
     * @throws Exception
     */
    public static void decodeToFile(String filePath, String base64) throws Exception {
        byte[] bytes = decode(base64);
        byteArrayToFile(bytes, filePath);
    }

    /**
     * ?
     * <p>
     * @param linuxDir
     *             linux
     * @param winDir
     *             win
     * @param fileName
     *             ??
     * @return String
     */
    public static String filePath(String linuxDir, String winDir, String fileName) {
        StringBuffer bf = new StringBuffer();
        if ("\\".equals(File.separator)) {
            //windows
            bf.append(winDir);
        } else if ("/".equals(File.separator)) {
            //Linux
            bf.append(linuxDir);
        }
        bf.append(File.separator);
        bf.append(fileName);
        return bf.toString();
    }

    /**
     * <p>
     * ?
     * </p>
     * 
     * @param filePath 
     * @return
     * @throws Exception
     */
    public static byte[] fileToByte(String filePath) throws Exception {
        byte[] data = new byte[0];
        File file = new File(filePath);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                out.write(cache, 0, nRead);
                out.flush();
            }
            out.close();
            in.close();
            data = out.toByteArray();
        }
        return data;
    }

    /**
     * <p>
     * ?
     * </p>
     * 
     * @param bytes ?
     * @param filePath ?
     */
    public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
        InputStream in = new ByteArrayInputStream(bytes);
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        destFile.createNewFile();
        OutputStream out = new FileOutputStream(destFile);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);
            out.flush();
        }
        out.close();
        in.close();
    }

}