001/**
002 *
003 * Copyright © 2014-2015 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;
018
019import java.io.UnsupportedEncodingException;
020
021import org.jivesoftware.smack.util.Objects;
022import org.jivesoftware.smack.util.StringUtils;
023
024public class Base64 {
025
026    private static Base64.Encoder base64encoder;
027
028    public static void setEncoder(Base64.Encoder encoder) {
029        Objects.requireNonNull(encoder, "encoder must no be null");
030        base64encoder = encoder;
031    }
032
033    public static final String encode(String string) {
034        try {
035            return encodeToString(string.getBytes(StringUtils.UTF8));
036        } catch (UnsupportedEncodingException e) {
037            throw new IllegalStateException("UTF-8 not supported", e);
038        }
039    }
040
041    public static final String encodeToString(byte[] input) {
042        byte[] bytes = encode(input);
043        try {
044            return new String(bytes, StringUtils.USASCII);
045        } catch (UnsupportedEncodingException e) {
046            throw new AssertionError(e);
047        }
048    }
049
050    public static final String encodeToString(byte[] input, int offset, int len) {
051        byte[] bytes = encode(input, offset, len);
052        try {
053            return new String(bytes, StringUtils.USASCII);
054        } catch (UnsupportedEncodingException e) {
055            throw new AssertionError(e);
056        }
057    }
058
059    public static final byte[] encode(byte[] input) {
060        return encode(input, 0, input.length);
061    }
062
063    public static final byte[] encode(byte[] input, int offset, int len) {
064        return base64encoder.encode(input, offset, len);
065    }
066
067    public static final String decodeToString(String string) {
068        byte[] bytes = decode(string);
069        try {
070            return new String(bytes, StringUtils.UTF8);
071        } catch (UnsupportedEncodingException e) {
072            throw new IllegalStateException("UTF-8 not supported", e);
073        }
074    }
075
076    public static final String decodeToString(byte[] input, int offset, int len) {
077        byte[] bytes = decode(input, offset, len);
078        try {
079            return new String(bytes, StringUtils.UTF8);
080        } catch (UnsupportedEncodingException e) {
081            throw new IllegalStateException("UTF-8 not supported", e);
082        }
083    }
084
085    public static final byte[] decode(String string) {
086        return base64encoder.decode(string);
087    }
088
089    public static final byte[] decode(byte[] input) {
090        return base64encoder.decode(input, 0, input.length);
091    }
092
093    public static final byte[] decode(byte[] input, int offset, int len) {
094        return base64encoder.decode(input, offset, len);
095    }
096
097    public interface Encoder {
098        byte[] decode(String string);
099
100        byte[] decode(byte[] input, int offset, int len);
101
102        String encodeToString(byte[] input, int offset, int len);
103
104        byte[] encode(byte[] input, int offset, int len);
105    }
106}