#18: added initial implementation that *should* work in Android environments. This still needs to be tested in a real Android project.

This commit is contained in:
Les Hazlewood 2015-04-03 19:06:42 -07:00
parent 060666a32e
commit e983155326
2 changed files with 42 additions and 4 deletions

16
pom.xml
View File

@ -106,6 +106,22 @@
<optional>true</optional>
</dependency>
<!-- Provided scope: only used in Android environments - not downloaded as a transitive dependency.
This dependency is only a stub of the actual implementation, which means we can't use it at test time
during TestNG tests. An Android environment is required to test for real. -->
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Test Dependencies: Only required for testing when building. Not required by users at runtime: -->
<dependency>
<groupId>ch.qos.logback</groupId>

View File

@ -15,18 +15,40 @@
*/
package io.jsonwebtoken.impl;
import javax.xml.bind.DatatypeConverter;
import io.jsonwebtoken.lang.Classes;
public class Base64Codec extends AbstractTextCodec {
@Override
private static final boolean STANDARD_JVM = Classes.isAvailable("javax.xml.bind.DatatypeConverter");
private static final boolean ANDROID = Classes.isAvailable("android.util.Base64");
public String encode(byte[] data) {
return DatatypeConverter.printBase64Binary(data);
if (STANDARD_JVM) {
return javax.xml.bind.DatatypeConverter.printBase64Binary(data);
}
if (ANDROID) {
int flags = android.util.Base64.NO_PADDING | android.util.Base64.NO_WRAP;
return android.util.Base64.encodeToString(data, flags);
}
throw new IllegalStateException("Unable to locate a Base64 codec for the current JVM");
}
@Override
public byte[] decode(String encoded) {
return DatatypeConverter.parseBase64Binary(encoded);
if (STANDARD_JVM) {
return javax.xml.bind.DatatypeConverter.parseBase64Binary(encoded);
}
if (ANDROID) {
return android.util.Base64.decode(encoded, android.util.Base64.DEFAULT);
}
throw new IllegalStateException("Unable to locate a Base64 codec for the current JVM");
}
}