Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit33e4b87

Browse files
committed
add Base64 implementation - JAXB v2.3.0 (the latest for JRE 7)
1 parent36c1c71 commit33e4b87

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
returnnewJava8Base64();
2424
}
25-
if (CommonsCodecBase64.isAvailable()) {
26-
returnnewCommonsCodecBase64();
25+
if (Jaxb230Base64.isAvailable()) {
26+
returnnewJaxb230Base64();
2727
}
2828
if (JaxbBase64.isAvailable()) {
2929
returnnewJaxbBase64();
3030
}
31+
if (CommonsCodecBase64.isAvailable()) {
32+
returnnewCommonsCodecBase64();
33+
}
3134
thrownewIllegalStateException(
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+
packagecom.github.scribejava.core.base64;
2+
3+
importjavax.xml.bind.DatatypeConverter;
4+
5+
/**
6+
* JAXB v2.3.0 (the latest for JRE 7)
7+
*/
8+
publicclassJaxb230Base64extendsBase64 {
9+
10+
@Override
11+
protectedStringinternalEncode(byte[]bytes) {
12+
returnDatatypeConverter.printBase64Binary(bytes);
13+
}
14+
15+
@Override
16+
protectedStringinternalEncodeUrlWithoutPadding(byte[]bytes) {
17+
Stringstring =DatatypeConverter.printBase64Binary(bytes);
18+
while (string.endsWith("=")) {
19+
string =string.substring(0,string.length() -1);
20+
}
21+
returnstring.replace('+','-').replace('/','_');
22+
}
23+
24+
staticbooleanisAvailable() {
25+
try {
26+
Class.forName("javax.xml.bind.DatatypeConverter",false,Jaxb230Base64.class.getClassLoader());
27+
returntrue;
28+
}catch (ClassNotFoundExceptioncnfE) {
29+
returnfalse;
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
privateBase64java8Base64;
1212
privateBase64commonsCodecBase64;
1313
privateBase64jaxbBase64;
14+
privateBase64jaxb230Base64;
1415
privatebyte[]helloWorldBytes;
1516
privatebyte[]helloWorldTwoLinesBytes;
1617
privatebyte[]helloWorldTwoLinesAndNewLineBytes;
@@ -57,13 +58,15 @@ public void setUp() throws UnsupportedEncodingException {
5758
java8Base64 =newJava8Base64();
5859
commonsCodecBase64 =newCommonsCodecBase64();
5960
jaxbBase64 =newJaxbBase64();
61+
jaxb230Base64 =newJaxb230Base64();
6062
}
6163

6264
@Test
6365
publicvoidallImplementationsAreAvailable() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp