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

devutility.internal.data.codec.Base64Helper Maven / Gradle / Ivy

There is a newer version: 1.3.8.1
Show newest version
package devutility.internal.data.codec;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Helper {
	// region encode

	public static byte[] encode(byte[] bytes) {
		if (bytes == null || bytes.length == 0) {
			return null;
		}

		return Base64.getEncoder().encode(bytes);
	}

	// endregion

	// region encode to String

	public static String encodeToString(byte[] bytes) {
		byte[] base64Bytes = encode(bytes);

		try {
			return UTF8Utils.decode(base64Bytes);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	// endregion

	// region decode

	public static byte[] decode(byte[] bytes) {
		if (bytes == null || bytes.length == 0) {
			return null;
		}

		return Base64.getDecoder().decode(bytes);
	}

	// endregion

	// region decode by String

	public static byte[] decodeByString(String value) {
		try {
			byte[] base64Bytes = UTF8Utils.encode(value);
			return decode(base64Bytes);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// endregion
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy