Base64Decoder.java

/*
 * Copyright (C) 2019 sw4j.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.sw4j.tool.barcode.random.decoder.impl;

import java.util.Base64;
import java.util.Locale;
import org.sw4j.tool.barcode.random.decoder.ByteArrayDecoder;

/**
 * <p>
 * This class decodes a base64 string as byte array.
 * </p>
 * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
 */
public class Base64Decoder extends ByteArrayDecoder {

    /**
     * <p>
     * The encoding name as constant.
     * </p>
     */
    public static final String TYPE = "base64";

    /**
     * <p>
     * The default constructor.
     * </p>
     */
    public Base64Decoder() {
    }

    /**
     * <p>
     * Return {@code true} if the {@code encoding} is base64 (case insensitive).
     * </p>
     * @param encoding the encoding name to check.
     * @return {@code true} if the name is base64.
     */
    @Override
    public boolean supports(final String encoding) {
        return encoding != null && TYPE.equals(encoding.toLowerCase(Locale.ROOT));
    }

    /**
     * <p>
     * Return the base64 string decoded as byte array.
     * </p>
     * @param data the string to encode.
     * @return the decoded data.
     */
    @Override
    public byte[] decode(final String data) {
        return Base64.getDecoder().decode(data);
    }

}