[Java/Utils] 基于Java实现Base64的编码与解码:Base64Utils

Z 最佳实践

Base64Utils

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 cn.xxx.xxx.common.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @reference-doc
 *  [1] org.springframework.util.Base64Utils
 *  [2] java.util.Base64
 */

/**
 * A wrapper of {@link java.util.Base64} with convenient conversion methods between {@code byte[]} and {@code String}
 */
public final class Base64Utils {
    public final static Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

    private static final java.util.Base64.Decoder DECODER = java.util.Base64.getDecoder();
    private static final java.util.Base64.Encoder ENCODER = java.util.Base64.getEncoder();

    private Base64Utils() {

    }

    /**
     * 密文解码为明文
     * @param cipherText
     * @return originText
     */
    public static String decode(String cipherText) {
        return decode( cipherText, DEFAULT_CHARSET );
    }

    public static String decode(String cipherText, Charset charset) {
        return new String(DECODER.decode(cipherText), charset);
    }

    /**
     * Base64-decode the given byte array.
     * @param src the encoded byte array
     * @return the original byte array
     */
    public static byte[] decode(byte[] src) {
        if (src.length == 0) {
            return src;
        }
        return Base64.getDecoder().decode(src);
    }

    /**
     * 明文编码为密文
     * @param originText
     * @return cipherText
     */
    public static String encode(String originText) {
        return encode(originText, DEFAULT_CHARSET);
    }
    public static String encode(String originText, Charset charset) {
        return ENCODER.encodeToString(originText.getBytes(charset));
    }

    /**
     * Base64-encode the given byte array.
     * @param src the original byte array
     * @return the encoded byte array
     */
    public static byte[] encode(byte[] src) {
        if (src.length == 0) {
            return src;
        }
        return Base64.getEncoder().encode(src);
    }

    /**
     * org.springframework.util.Base64Utils's encode  and decode method:
     */

    /**
     * Base64-encode the given byte array to a String.
     * @param src the original byte array
     * @return the encoded byte array as a UTF-8 String
     */
//    public static String encodeToString(byte[] src, Charset charset) {
//        if (src.length == 0) {
//            return "";
//        }
//        return new String(encode(src), charset);
//    }
//    public static String encodeToString(byte[] src) {
//        return encodeToString(src, DEFAULT_CHARSET);
//    }

    /**
     * Base64-decode the given byte array from an UTF-8 String.
     * @param src the encoded UTF-8 String
     * @return the original byte array
     */
//    public static byte[] decodeFromString(String src, Charset charset) {
//        if (src.isEmpty()) {
//            return new byte[0];
//        }
//        return decode(src.getBytes(charset));
//    }
//    public static byte[] decodeFromString(String src) {
//        return decodeFromString(src, DEFAULT_CHARSET);
//    }
}

X 参考文献

  • org.springframework.util.Base64Utils
  • java.util.Base64
posted @ 2025-09-10 23:50  千千寰宇  阅读(11)  评论(0)    收藏  举报