Skip to content

Commit 33e4b87

Browse files
committed
add Base64 implementation - JAXB v2.3.0 (the latest for JRE 7)
1 parent 36c1c71 commit 33e4b87

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

scribejava-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
<version>3.0.0</version>
3333
<optional>true</optional>
3434
</dependency>
35+
<dependency>
36+
<groupId>javax.xml.bind</groupId>
37+
<artifactId>jaxb-api</artifactId>
38+
<version>2.3.0</version> <!-- the latest for the JRE 7 -->
39+
<optional>true</optional>
40+
</dependency>
3541
</dependencies>
3642

3743
<build>

scribejava-core/src/main/java/com/github/scribejava/core/base64/Base64.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ private static Base64 createInstance() {
2222
if (Java8Base64.isAvailable()) {
2323
return new Java8Base64();
2424
}
25-
if (CommonsCodecBase64.isAvailable()) {
26-
return new CommonsCodecBase64();
25+
if (Jaxb230Base64.isAvailable()) {
26+
return new Jaxb230Base64();
2727
}
2828
if (JaxbBase64.isAvailable()) {
2929
return new JaxbBase64();
3030
}
31+
if (CommonsCodecBase64.isAvailable()) {
32+
return new CommonsCodecBase64();
33+
}
3134
throw new IllegalStateException(
3235
"No Base64 implementation was provided. Java 8 Base64, Apache Commons Codec or JAXB is needed");
3336
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.scribejava.core.base64;
2+
3+
import javax.xml.bind.DatatypeConverter;
4+
5+
/**
6+
* JAXB v2.3.0 (the latest for JRE 7)
7+
*/
8+
public class Jaxb230Base64 extends Base64 {
9+
10+
@Override
11+
protected String internalEncode(byte[] bytes) {
12+
return DatatypeConverter.printBase64Binary(bytes);
13+
}
14+
15+
@Override
16+
protected String internalEncodeUrlWithoutPadding(byte[] bytes) {
17+
String string = DatatypeConverter.printBase64Binary(bytes);
18+
while (string.endsWith("=")) {
19+
string = string.substring(0, string.length() - 1);
20+
}
21+
return string.replace('+', '-').replace('/', '_');
22+
}
23+
24+
static boolean isAvailable() {
25+
try {
26+
Class.forName("javax.xml.bind.DatatypeConverter", false, Jaxb230Base64.class.getClassLoader());
27+
return true;
28+
} catch (ClassNotFoundException cnfE) {
29+
return false;
30+
}
31+
}
32+
}

scribejava-core/src/test/java/com/github/scribejava/core/base64/Base64Test.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Base64Test {
1111
private Base64 java8Base64;
1212
private Base64 commonsCodecBase64;
1313
private Base64 jaxbBase64;
14+
private Base64 jaxb230Base64;
1415
private byte[] helloWorldBytes;
1516
private byte[] helloWorldTwoLinesBytes;
1617
private byte[] helloWorldTwoLinesAndNewLineBytes;
@@ -57,13 +58,15 @@ public void setUp() throws UnsupportedEncodingException {
5758
java8Base64 = new Java8Base64();
5859
commonsCodecBase64 = new CommonsCodecBase64();
5960
jaxbBase64 = new JaxbBase64();
61+
jaxb230Base64 = new Jaxb230Base64();
6062
}
6163

6264
@Test
6365
public void allImplementationsAreAvailable() {
6466
assertTrue(Java8Base64.isAvailable());
6567
assertTrue(CommonsCodecBase64.isAvailable());
6668
assertTrue(JaxbBase64.isAvailable());
69+
assertTrue(Jaxb230Base64.isAvailable());
6770
}
6871

6972
@Test
@@ -109,6 +112,13 @@ public void testEncode() {
109112
assertEquals(helloWorldTwoLinesAndNewLineEncoded, jaxbBase64.internalEncode(helloWorldTwoLinesAndNewLineBytes));
110113
assertEquals(helloWorldDifferentCharsEncoded, jaxbBase64.internalEncode(helloWorldDifferentCharsBytes));
111114
assertEquals(str, jaxbBase64.internalEncode(bytes));
115+
116+
assertEquals(helloWorldEncoded, jaxb230Base64.internalEncode(helloWorldBytes));
117+
assertEquals(helloWorldTwoLinesEncoded, jaxb230Base64.internalEncode(helloWorldTwoLinesBytes));
118+
assertEquals(helloWorldTwoLinesAndNewLineEncoded,
119+
jaxb230Base64.internalEncode(helloWorldTwoLinesAndNewLineBytes));
120+
assertEquals(helloWorldDifferentCharsEncoded, jaxb230Base64.internalEncode(helloWorldDifferentCharsBytes));
121+
assertEquals(str, jaxb230Base64.internalEncode(bytes));
112122
}
113123

114124
@Test
@@ -159,5 +169,13 @@ public void testEncodeUrlWithoutPadding() {
159169
assertEquals(helloWorldDifferentCharsEncoded,
160170
jaxbBase64.internalEncodeUrlWithoutPadding(helloWorldDifferentCharsBytes));
161171
assertEquals(str, jaxbBase64.internalEncodeUrlWithoutPadding(bytes));
172+
173+
assertEquals(helloWorldEncoded, jaxb230Base64.internalEncodeUrlWithoutPadding(helloWorldBytes));
174+
assertEquals(helloWorldTwoLinesEncoded, jaxb230Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesBytes));
175+
assertEquals(helloWorldTwoLinesAndNewLineEncoded,
176+
jaxb230Base64.internalEncodeUrlWithoutPadding(helloWorldTwoLinesAndNewLineBytes));
177+
assertEquals(helloWorldDifferentCharsEncoded,
178+
jaxb230Base64.internalEncodeUrlWithoutPadding(helloWorldDifferentCharsBytes));
179+
assertEquals(str, jaxb230Base64.internalEncodeUrlWithoutPadding(bytes));
162180
}
163181
}

0 commit comments

Comments
 (0)