001/**
002 *
003 * Copyright © 2014-2021 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util.stringencoder.android;
018
019import android.util.Base64;
020
021/**
022 * A Base 64 encoding implementation based on android.util.Base64.
023 * @author Florian Schmaus
024 */
025public final class AndroidBase64Encoder implements org.jivesoftware.smack.util.stringencoder.Base64.Encoder {
026
027    /**
028     * An instance of this encoder.
029     */
030    public static AndroidBase64Encoder INSTANCE = new AndroidBase64Encoder();
031
032    private static final int BASE64_ENCODER_FLAGS = Base64.NO_WRAP;
033
034    private AndroidBase64Encoder() {
035        // Use getInstance()
036    }
037
038    @Override
039    public byte[] decode(String string) {
040        return Base64.decode(string, Base64.DEFAULT);
041    }
042
043    @Override
044    public String encodeToString(byte[] input) {
045        return Base64.encodeToString(input, BASE64_ENCODER_FLAGS);
046    }
047
048    @Override
049    public String encodeToStringWithoutPadding(byte[] input) {
050        return Base64.encodeToString(input, BASE64_ENCODER_FLAGS | Base64.NO_PADDING);
051    }
052
053    @Override
054    public byte[] encode(byte[] input) {
055        return Base64.encode(input, BASE64_ENCODER_FLAGS);
056    }
057}