Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify encode/decode base64 #180

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -44,6 +44,7 @@ Check out all the resources and all the Java code examples in the [Official Docu
- [SMS API](#sms-api)
- [Token authentication](#token-authentication)
- [Example Request](#example-request)
- [Other Examples](#other-examples)
- [Contribute](#contribute)


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

## Other examples

- [AWS Lambda](https://github.com/fouad-j/aws-lambda-java-jetmail)

## Contribute

Mailjet loves developers. You can be part of this project!
Expand Down
58 changes: 6 additions & 52 deletions src/main/java/com/mailjet/client/Base64.java
@@ -1,17 +1,9 @@
package com.mailjet.client;


public class Base64 {

private final static char[] ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
import static java.nio.charset.StandardCharsets.UTF_8;

private static int[] toInt = new int[128];

static {
for(int i=0; i< ALPHABET.length; i++){
toInt[ALPHABET[i]]= i;
}
}
public class Base64 {

/**
* Translates the specified byte array into Base64 string.
Expand All @@ -20,26 +12,7 @@ public class Base64 {
* @return the translated Base64 string (not null)
*/
public static String encode(byte[] buf){
int size = buf.length;
char[] ar = new char[((size + 2) / 3) * 4];
int a = 0;
int i=0;
while(i < size){
byte b0 = buf[i++];
byte b1 = (i < size) ? buf[i++] : 0;
byte b2 = (i < size) ? buf[i++] : 0;

int mask = 0x3F;
ar[a++] = ALPHABET[(b0 >> 2) & mask];
ar[a++] = ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
ar[a++] = ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
ar[a++] = ALPHABET[b2 & mask];
}
switch(size % 3){
case 1: ar[--a] = '=';
case 2: ar[--a] = '=';
}
return new String(ar);
return new String(java.util.Base64.getEncoder().encode(buf), UTF_8);
}

/**
Expand All @@ -49,26 +22,7 @@ public static String encode(byte[] buf){
* @return the byte array (not null)
*/
public static byte[] decode(String s){
int delta = s.endsWith( "==" ) ? 2 : s.endsWith( "=" ) ? 1 : 0;
byte[] buffer = new byte[s.length()*3/4 - delta];
int mask = 0xFF;
int index = 0;
for(int i=0; i< s.length(); i+=4){
int c0 = toInt[s.charAt( i )];
int c1 = toInt[s.charAt( i + 1)];
buffer[index++]= (byte)(((c0 << 2) | (c1 >> 4)) & mask);
if(index >= buffer.length){
return buffer;
}
int c2 = toInt[s.charAt( i + 2)];
buffer[index++]= (byte)(((c1 << 4) | (c2 >> 2)) & mask);
if(index >= buffer.length){
return buffer;
}
int c3 = toInt[s.charAt( i + 3 )];
buffer[index++]= (byte)(((c2 << 6) | c3) & mask);
}
return buffer;
}
return java.util.Base64.getDecoder().decode(s);
}

}
}
30 changes: 30 additions & 0 deletions src/test/java/com/mailjet/client/Base64Test.java
@@ -0,0 +1,30 @@
package com.mailjet.client;

import junit.framework.TestCase;
import org.junit.Assert;

import static java.nio.charset.StandardCharsets.UTF_8;

public class Base64Test extends TestCase {
public void test_should_base64_decode_provided_string() {
// GIVEN
String mock = "dGVzdDp0ZXN0w6lhJCTDoCk9Lyo=";

// WHEN
byte[] result = Base64.decode(mock);

// THEN
Assert.assertEquals("test:testéa$$à)=/*", new String(result));
}

public void test_should_base64_encode_provided_bytes_array() {
// GIVEN
byte[] mock = "test:testéa$$à)=/*".getBytes(UTF_8);

// WHEN
String result = Base64.encode(mock);

// THEN
Assert.assertEquals("dGVzdDp0ZXN0w6lhJCTDoCk9Lyo=", result);
}
}