001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util;
018
019import java.nio.charset.Charset;
020import java.nio.charset.StandardCharsets;
021import java.util.Base64;
022
023/**
024 * A simple utility class for Base64 encoding and decoding.
025 *
026 * <p>Adapts to Java 8's {@link java.util.Base64} in a convenience fashion.
027 *
028 * @author Juergen Hoeller
029 * @author Gary Russell
030 * @since 4.1
031 * @see java.util.Base64
032 */
033public abstract class Base64Utils {
034
035        private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
036
037
038        /**
039         * Base64-encode the given byte array.
040         * @param src the original byte array
041         * @return the encoded byte array
042         */
043        public static byte[] encode(byte[] src) {
044                if (src.length == 0) {
045                        return src;
046                }
047                return Base64.getEncoder().encode(src);
048        }
049
050        /**
051         * Base64-decode the given byte array.
052         * @param src the encoded byte array
053         * @return the original byte array
054         */
055        public static byte[] decode(byte[] src) {
056                if (src.length == 0) {
057                        return src;
058                }
059                return Base64.getDecoder().decode(src);
060        }
061
062        /**
063         * Base64-encode the given byte array using the RFC 4648
064         * "URL and Filename Safe Alphabet".
065         * @param src the original byte array
066         * @return the encoded byte array
067         * @since 4.2.4
068         */
069        public static byte[] encodeUrlSafe(byte[] src) {
070                if (src.length == 0) {
071                        return src;
072                }
073                return Base64.getUrlEncoder().encode(src);
074        }
075
076        /**
077         * Base64-decode the given byte array using the RFC 4648
078         * "URL and Filename Safe Alphabet".
079         * @param src the encoded byte array
080         * @return the original byte array
081         * @since 4.2.4
082         */
083        public static byte[] decodeUrlSafe(byte[] src) {
084                if (src.length == 0) {
085                        return src;
086                }
087                return Base64.getUrlDecoder().decode(src);
088        }
089
090        /**
091         * Base64-encode the given byte array to a String.
092         * @param src the original byte array
093         * @return the encoded byte array as a UTF-8 String
094         */
095        public static String encodeToString(byte[] src) {
096                if (src.length == 0) {
097                        return "";
098                }
099                return new String(encode(src), DEFAULT_CHARSET);
100        }
101
102        /**
103         * Base64-decode the given byte array from an UTF-8 String.
104         * @param src the encoded UTF-8 String
105         * @return the original byte array
106         */
107        public static byte[] decodeFromString(String src) {
108                if (src.isEmpty()) {
109                        return new byte[0];
110                }
111                return decode(src.getBytes(DEFAULT_CHARSET));
112        }
113
114        /**
115         * Base64-encode the given byte array to a String using the RFC 4648
116         * "URL and Filename Safe Alphabet".
117         * @param src the original byte array
118         * @return the encoded byte array as a UTF-8 String
119         */
120        public static String encodeToUrlSafeString(byte[] src) {
121                return new String(encodeUrlSafe(src), DEFAULT_CHARSET);
122        }
123
124        /**
125         * Base64-decode the given byte array from an UTF-8 String using the RFC 4648
126         * "URL and Filename Safe Alphabet".
127         * @param src the encoded UTF-8 String
128         * @return the original byte array
129         */
130        public static byte[] decodeFromUrlSafeString(String src) {
131                return decodeUrlSafe(src.getBytes(DEFAULT_CHARSET));
132        }
133
134}