All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.commons.codec.Base64s Maven / Gradle / Ivy

There is a newer version: 12.0.2
Show newest version
package net.dongliu.commons.codec;

import javax.annotation.concurrent.Immutable;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Objects;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Convenient method for encode/deocde base64
 *
 * @author Liu Dong
 */
public class Base64s {

    /**
     * Create plain base64
     */
    public static Base64Holder plain() {
        return Base64Holder.plain;
    }

    /**
     * Create mine base64
     */
    public static Base64Holder mime() {
        return Base64Holder.mime;
    }

    /**
     * Create url safe base64
     */
    public static Base64Holder urlSafe() {
        return Base64Holder.urlSafe;
    }


    /**
     * Base64 encoder/decoder holder
     */
    @Immutable
    public static class Base64Holder {
        private final Base64.Encoder encoder;
        private final Base64.Decoder decoder;

        private static final Base64Holder plain = new Base64Holder(Base64.getEncoder(), Base64.getDecoder());
        private static final Base64Holder mime = new Base64Holder(Base64.getMimeEncoder(), Base64.getMimeDecoder());
        private static final Base64Holder urlSafe = new Base64Holder(Base64.getUrlEncoder(), Base64.getUrlDecoder());

        private Base64Holder(Base64.Encoder encoder, Base64.Decoder decoder) {
            this.encoder = encoder;
            this.decoder = decoder;
        }

        /**
         * Encode byte array data
         */
        public Base64Encoder encode(byte[] data) {
            return new Base64Encoder(encoder, data);
        }

        /**
         * Encode byte buffer data
         */
        public Base64Encoder encode(ByteBuffer buffer) {
            byte[] data;
            if (buffer.hasArray()) {
                data = buffer.array();
            } else {
                data = new byte[buffer.remaining()];
                buffer.get(data);
            }
            return encode(data);
        }

        /**
         * Encode data(provided by str with charset) to base64 string
         */
        public Base64Encoder encode(String str, Charset charset) {
            return encode(str.getBytes(charset));
        }

        /**
         * Encode data(provided by str with UTF-8 charset) to base64 string
         */
        public Base64Encoder encode(String str) {
            return encode(str, UTF_8);
        }


        /**
         * Decode base 64 data
         */
        public Base64Decoder decode(byte[] data) {
            return new Base64Decoder(decoder, data);
        }

        /**
         * Decode base 64 data
         */
        public Base64Decoder decode(String data) {
            return decode(data.getBytes(ISO_8859_1));
        }
    }

    /**
     * Hold base64 encode info
     */
    @Immutable
    public static class Base64Encoder {
        private final Base64.Encoder encoder;
        private final byte[] src;

        public Base64Encoder(Base64.Encoder encoder, byte[] src) {
            this.encoder = encoder;
            this.src = Objects.requireNonNull(src);
        }

        /**
         * Encode data to base64 string
         */
        public String toBase64String() {
            return encoder.encodeToString(Objects.requireNonNull(src));
        }

        /**
         * Encode data to base64 byte array
         */
        public byte[] toByteArray() {
            return encoder.encode(Objects.requireNonNull(src));
        }

        /**
         * Encode data to base64 byte array
         */
        public void toByteArray(byte[] dst) {
            encoder.encode(Objects.requireNonNull(src), dst);
        }
    }

    /**
     * Hold base64 decode info
     */
    @Immutable
    public static class Base64Decoder {
        private final Base64.Decoder decoder;
        private final byte[] base64Data;

        public Base64Decoder(Base64.Decoder decoder, byte[] base64Data) {
            this.decoder = decoder;
            this.base64Data = Objects.requireNonNull(base64Data);
        }

        /**
         * Decode base64 data to byte array
         */
        public byte[] toByteArray() {
            return decoder.decode(base64Data);
        }

        /**
         * Decode base64 data to byte array
         */
        public void toByteArray(byte[] dst) {
            decoder.decode(base64Data, dst);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy