Skip to content

Commit 1dc6a6c

Browse files
committed
Merge branch 'master' of github.com-mdmytrash:mailjet/mailjet-apiv3-java
2 parents ba33823 + 528a6a6 commit 1dc6a6c

3 files changed

Lines changed: 41 additions & 52 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Check out all the resources and all the Java code examples in the [Official Docu
4444
- [SMS API](#sms-api)
4545
- [Token authentication](#token-authentication)
4646
- [Example Request](#example-request)
47+
- [Other Examples](#other-examples)
4748
- [Contribute](#contribute)
4849

4950

@@ -590,6 +591,10 @@ Assert.assertEquals("Message is being sent", response.getData().getJSONObject(0)
590591
```
591592
Also, you can check [integration tests](src/test/java/com/mailjet/client/SendSmsIT.java) how to work with Mailjet client.
592593

594+
## Other examples
595+
596+
- [AWS Lambda](https://github.com/fouad-j/aws-lambda-java-jetmail)
597+
593598
## Contribute
594599

595600
Mailjet loves developers. You can be part of this project!
Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package com.mailjet.client;
22

33

4-
public class Base64 {
5-
6-
private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
4+
import static java.nio.charset.StandardCharsets.UTF_8;
75

8-
private static int[] toInt = new int[128];
9-
10-
static {
11-
for(int i=0; i< ALPHABET.length; i++){
12-
toInt[ALPHABET[i]]= i;
13-
}
14-
}
6+
public class Base64 {
157

168
/**
179
* Translates the specified byte array into Base64 string.
@@ -20,26 +12,7 @@ public class Base64 {
2012
* @return the translated Base64 string (not null)
2113
*/
2214
public static String encode(byte[] buf){
23-
int size = buf.length;
24-
char[] ar = new char[((size + 2) / 3) * 4];
25-
int a = 0;
26-
int i=0;
27-
while(i < size){
28-
byte b0 = buf[i++];
29-
byte b1 = (i < size) ? buf[i++] : 0;
30-
byte b2 = (i < size) ? buf[i++] : 0;
31-
32-
int mask = 0x3F;
33-
ar[a++] = ALPHABET[(b0 >> 2) & mask];
34-
ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
35-
ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
36-
ar[a++] = ALPHABET[b2 & mask];
37-
}
38-
switch(size % 3){
39-
case 1: ar[--a] = '=';
40-
case 2: ar[--a] = '=';
41-
}
42-
return new String(ar);
15+
return new String(java.util.Base64.getEncoder().encode(buf), UTF_8);
4316
}
4417

4518
/**
@@ -49,26 +22,7 @@ public static String encode(byte[] buf){
4922
* @return the byte array (not null)
5023
*/
5124
public static byte[] decode(String s){
52-
int delta = s.endsWith( "==" ) ? 2 : s.endsWith( "=" ) ? 1 : 0;
53-
byte[] buffer = new byte[s.length()*3/4 - delta];
54-
int mask = 0xFF;
55-
int index = 0;
56-
for(int i=0; i< s.length(); i+=4){
57-
int c0 = toInt[s.charAt( i )];
58-
int c1 = toInt[s.charAt( i + 1)];
59-
buffer[index++]= (byte)(((c0 << 2) | (c1 >> 4)) & mask);
60-
if(index >= buffer.length){
61-
return buffer;
62-
}
63-
int c2 = toInt[s.charAt( i + 2)];
64-
buffer[index++]= (byte)(((c1 << 4) | (c2 >> 2)) & mask);
65-
if(index >= buffer.length){
66-
return buffer;
67-
}
68-
int c3 = toInt[s.charAt( i + 3 )];
69-
buffer[index++]= (byte)(((c2 << 6) | c3) & mask);
70-
}
71-
return buffer;
72-
}
25+
return java.util.Base64.getDecoder().decode(s);
26+
}
7327

74-
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mailjet.client;
2+
3+
import junit.framework.TestCase;
4+
import org.junit.Assert;
5+
6+
import static java.nio.charset.StandardCharsets.UTF_8;
7+
8+
public class Base64Test extends TestCase {
9+
public void test_should_base64_decode_provided_string() {
10+
// GIVEN
11+
String mock = "dGVzdDp0ZXN0w6lhJCTDoCk9Lyo=";
12+
13+
// WHEN
14+
byte[] result = Base64.decode(mock);
15+
16+
// THEN
17+
Assert.assertEquals("test:testéa$$à)=/*", new String(result));
18+
}
19+
20+
public void test_should_base64_encode_provided_bytes_array() {
21+
// GIVEN
22+
byte[] mock = "test:testéa$$à)=/*".getBytes(UTF_8);
23+
24+
// WHEN
25+
String result = Base64.encode(mock);
26+
27+
// THEN
28+
Assert.assertEquals("dGVzdDp0ZXN0w6lhJCTDoCk9Lyo=", result);
29+
}
30+
}

0 commit comments

Comments
 (0)