2014-09-04 11:04:51 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-04-08 23:03:12 +02:00
|
|
|
* Copyright © 2014-2019 Florian Schmaus
|
2014-09-04 11:04:51 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smack.util.stringencoder;
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
2015-01-26 20:36:55 +01:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2014-09-04 11:04:51 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
|
|
|
|
|
|
|
public class Base64 {
|
|
|
|
|
|
|
|
private static Base64.Encoder base64encoder;
|
|
|
|
|
|
|
|
public static void setEncoder(Base64.Encoder encoder) {
|
2015-01-26 20:36:55 +01:00
|
|
|
Objects.requireNonNull(encoder, "encoder must no be null");
|
2014-09-04 11:04:51 +02:00
|
|
|
base64encoder = encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final String encode(String string) {
|
|
|
|
try {
|
|
|
|
return encodeToString(string.getBytes(StringUtils.UTF8));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new IllegalStateException("UTF-8 not supported", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final String encodeToString(byte[] input) {
|
2019-04-08 23:09:12 +02:00
|
|
|
return base64encoder.encodeToString(input);
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final String encodeToString(byte[] input, int offset, int len) {
|
2019-04-08 23:03:12 +02:00
|
|
|
return encodeToString(slice(input, offset, len));
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 23:09:12 +02:00
|
|
|
public static final String encodeToStringWithoutPadding(byte[] input) {
|
|
|
|
return base64encoder.encodeToStringWithoutPadding(input);
|
|
|
|
}
|
|
|
|
|
2014-09-04 11:04:51 +02:00
|
|
|
public static final byte[] encode(byte[] input) {
|
2019-04-08 23:03:12 +02:00
|
|
|
return base64encoder.encode(input);
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final String decodeToString(String string) {
|
|
|
|
byte[] bytes = decode(string);
|
|
|
|
try {
|
|
|
|
return new String(bytes, StringUtils.UTF8);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new IllegalStateException("UTF-8 not supported", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 23:03:12 +02:00
|
|
|
// TODO: We really should not mask the IllegalArgumentException. But some unit test depend on this behavior, like
|
|
|
|
// ibb.packet.DataPacketExtension.shouldReturnNullIfDataIsInvalid().
|
|
|
|
public static final byte[] decode(String string) {
|
2014-09-04 11:04:51 +02:00
|
|
|
try {
|
2019-04-08 23:03:12 +02:00
|
|
|
return base64encoder.decode(string);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return null;
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final byte[] decode(byte[] input) {
|
2019-04-08 23:03:12 +02:00
|
|
|
String string;
|
|
|
|
try {
|
|
|
|
string = new String(input, StringUtils.USASCII);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
return decode(string);
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 23:03:12 +02:00
|
|
|
private static byte[] slice(byte[] input, int offset, int len) {
|
|
|
|
if (offset == 0 && len == input.length) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] res = new byte[len];
|
|
|
|
System.arraycopy(input, offset, res, 0, len);
|
|
|
|
return res;
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public interface Encoder {
|
|
|
|
byte[] decode(String string);
|
|
|
|
|
2019-04-08 23:03:12 +02:00
|
|
|
String encodeToString(byte[] input);
|
2014-09-04 11:04:51 +02:00
|
|
|
|
2019-04-08 23:09:12 +02:00
|
|
|
String encodeToStringWithoutPadding(byte[] input);
|
|
|
|
|
2019-04-08 23:03:12 +02:00
|
|
|
byte[] encode(byte[] input);
|
2014-09-04 11:04:51 +02:00
|
|
|
}
|
|
|
|
}
|