mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 06:12:05 +01:00
Use java.util.Base64 and remove build-in Base64 API
This commit is contained in:
parent
0d17f195b0
commit
7d5274dad1
7 changed files with 57 additions and 1802 deletions
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
* Copyright © 2014-2019 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -42,18 +42,12 @@ public final class AndroidBase64Encoder implements org.jivesoftware.smack.util.s
|
|||
}
|
||||
|
||||
@Override
|
||||
public byte[] decode(byte[] input, int offset, int len) {
|
||||
return Base64.decode(input, offset, len, 0);
|
||||
public String encodeToString(byte[] input) {
|
||||
return Base64.encodeToString(input, BASE64_ENCODER_FLAGS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encodeToString(byte[] input, int offset, int len) {
|
||||
return Base64.encodeToString(input, offset, len, BASE64_ENCODER_FLAGS);
|
||||
public byte[] encode(byte[] input) {
|
||||
return Base64.encode(input, BASE64_ENCODER_FLAGS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] encode(byte[] input, int offset, int len) {
|
||||
return Base64.encode(input, offset, len, BASE64_ENCODER_FLAGS);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ dependencies {
|
|||
testCompile "org.powermock:powermock-module-junit4-rule:$powerMockVersion"
|
||||
testCompile "org.powermock:powermock-api-mockito2:$powerMockVersion"
|
||||
testCompile 'com.jamesmurty.utils:java-xmlbuilder:1.2'
|
||||
testCompile 'net.iharder:base64:2.3.8'
|
||||
}
|
||||
|
||||
class CreateFileTask extends DefaultTask {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014-2015 Florian Schmaus
|
||||
* Copyright © 2014-2019 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -48,20 +48,15 @@ public class Base64 {
|
|||
}
|
||||
|
||||
public static final String encodeToString(byte[] input, int offset, int len) {
|
||||
byte[] bytes = encode(input, offset, len);
|
||||
try {
|
||||
return new String(bytes, StringUtils.USASCII);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return encodeToString(slice(input, offset, len));
|
||||
}
|
||||
|
||||
public static final byte[] encode(byte[] input) {
|
||||
return encode(input, 0, input.length);
|
||||
return base64encoder.encode(input);
|
||||
}
|
||||
|
||||
public static final byte[] encode(byte[] input, int offset, int len) {
|
||||
return base64encoder.encode(input, offset, len);
|
||||
return encode(slice(input, offset, len));
|
||||
}
|
||||
|
||||
public static final String decodeToString(String string) {
|
||||
|
@ -73,34 +68,41 @@ public class Base64 {
|
|||
}
|
||||
}
|
||||
|
||||
public static final String decodeToString(byte[] input, int offset, int len) {
|
||||
byte[] bytes = decode(input, offset, len);
|
||||
// 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) {
|
||||
try {
|
||||
return new String(bytes, StringUtils.UTF8);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new IllegalStateException("UTF-8 not supported", e);
|
||||
return base64encoder.decode(string);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static final byte[] decode(String string) {
|
||||
return base64encoder.decode(string);
|
||||
}
|
||||
|
||||
public static final byte[] decode(byte[] input) {
|
||||
return base64encoder.decode(input, 0, input.length);
|
||||
String string;
|
||||
try {
|
||||
string = new String(input, StringUtils.USASCII);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return decode(string);
|
||||
}
|
||||
|
||||
public static final byte[] decode(byte[] input, int offset, int len) {
|
||||
return base64encoder.decode(input, offset, len);
|
||||
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;
|
||||
}
|
||||
|
||||
public interface Encoder {
|
||||
byte[] decode(String string);
|
||||
|
||||
byte[] decode(byte[] input, int offset, int len);
|
||||
String encodeToString(byte[] input);
|
||||
|
||||
String encodeToString(byte[] input, int offset, int len);
|
||||
|
||||
byte[] encode(byte[] input, int offset, int len);
|
||||
byte[] encode(byte[] input);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2014 Florian Schmaus
|
||||
* Copyright © 2014-2019 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,14 +16,10 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.test.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.stringencoder.Base64.Encoder;
|
||||
|
||||
import net.iharder.Base64;
|
||||
|
||||
/**
|
||||
* The SmackTestSuite takes care of initializing Smack for the unit tests. For example the Base64
|
||||
* encoder is configured.
|
||||
|
@ -35,53 +31,19 @@ public class SmackTestSuite {
|
|||
|
||||
@Override
|
||||
public byte[] decode(String string) {
|
||||
try {
|
||||
return Base64.decode(string);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// Expected by e.g. the unit test.
|
||||
// " Base64-encoded string must have at least four characters, but length specified was 1",
|
||||
// should not cause an exception, but instead null should be returned. Maybe
|
||||
// this should be changed in a later Smack release, so that the actual exception
|
||||
// is handled.
|
||||
return null;
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return Base64.getDecoder().decode(string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] decode(byte[] input, int offset, int len) {
|
||||
try {
|
||||
return Base64.decode(input, offset, len, 0);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// Expected by e.g. the unit test.
|
||||
// " Base64-encoded string must have at least four characters, but length specified was 1",
|
||||
// should not cause an exception, but instead null should be returned. Maybe
|
||||
// this should be changed in a later Smack release, so that the actual exception
|
||||
// is handled.
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
public String encodeToString(byte[] input) {
|
||||
return Base64.getEncoder().encodeToString(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encodeToString(byte[] input, int offset, int len) {
|
||||
return Base64.encodeBytes(input, offset, len);
|
||||
public byte[] encode(byte[] input) {
|
||||
return Base64.getEncoder().encode(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] encode(byte[] input, int offset, int len) {
|
||||
String string = encodeToString(input, offset, len);
|
||||
try {
|
||||
return string.getBytes(StringUtils.USASCII);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -16,9 +16,7 @@
|
|||
*/
|
||||
package org.jivesoftware.smack.util.stringencoder.java7;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* A Base 64 encoding implementation.
|
||||
|
@ -28,10 +26,12 @@ public final class Java7Base64Encoder implements org.jivesoftware.smack.util.str
|
|||
|
||||
private static final Java7Base64Encoder instance = new Java7Base64Encoder();
|
||||
|
||||
private static final int BASE64_ENCODER_FLAGS = Base64.DONT_BREAK_LINES;
|
||||
private final Base64.Encoder encoder;
|
||||
private final Base64.Decoder decoder;
|
||||
|
||||
private Java7Base64Encoder() {
|
||||
// Use getInstance()
|
||||
encoder = Base64.getEncoder();
|
||||
decoder = Base64.getDecoder();
|
||||
}
|
||||
|
||||
public static Java7Base64Encoder getInstance() {
|
||||
|
@ -40,27 +40,16 @@ public final class Java7Base64Encoder implements org.jivesoftware.smack.util.str
|
|||
|
||||
@Override
|
||||
public byte[] decode(String string) {
|
||||
return Base64.decode(string);
|
||||
return decoder.decode(string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] decode(byte[] input, int offset, int len) {
|
||||
return Base64.decode(input, offset, len, 0);
|
||||
public String encodeToString(byte[] input) {
|
||||
return encoder.encodeToString(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encodeToString(byte[] input, int offset, int len) {
|
||||
return Base64.encodeBytes(input, offset, len, BASE64_ENCODER_FLAGS);
|
||||
public byte[] encode(byte[] input) {
|
||||
return encoder.encode(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] encode(byte[] input, int offset, int len) {
|
||||
String string = encodeToString(input, offset, len);
|
||||
try {
|
||||
return string.getBytes(StringUtils.USASCII);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.jivesoftware.smack.util.stringencoder.java7;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.stringencoder.StringEncoder;
|
||||
|
@ -37,10 +38,12 @@ public final class Java7Base64UrlSafeEncoder implements StringEncoder<String> {
|
|||
|
||||
private static final Java7Base64UrlSafeEncoder instance = new Java7Base64UrlSafeEncoder();
|
||||
|
||||
private static final int BASE64_ENCODER_FLAGS = Base64.URL_SAFE | Base64.DONT_BREAK_LINES;
|
||||
private final Base64.Encoder encoder;
|
||||
private final Base64.Decoder decoder;
|
||||
|
||||
private Java7Base64UrlSafeEncoder() {
|
||||
// Use getInstance()
|
||||
encoder = Base64.getUrlEncoder();
|
||||
decoder = Base64.getUrlDecoder();
|
||||
}
|
||||
|
||||
public static Java7Base64UrlSafeEncoder getInstance() {
|
||||
|
@ -56,13 +59,14 @@ public final class Java7Base64UrlSafeEncoder implements StringEncoder<String> {
|
|||
catch (UnsupportedEncodingException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return Base64.encodeBytes(bytes, BASE64_ENCODER_FLAGS);
|
||||
return encoder.encodeToString(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String decode(String s) {
|
||||
byte[] bytes = decoder.decode(s);
|
||||
try {
|
||||
return new String(Base64.decode(s, BASE64_ENCODER_FLAGS), StringUtils.UTF8);
|
||||
return new String(bytes, StringUtils.UTF8);
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
throw new AssertionError(e);
|
||||
|
|
Loading…
Reference in a new issue