Skip to content

Fix byte array payload encoding, add more comprehensive tests. #47

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

Merged
merged 1 commit into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/src/androidTest/java/com/cloudinary/android/PayloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static junit.framework.Assert.assertEquals;
Expand All @@ -34,7 +35,7 @@ public static void setup() throws IOException {
@Test
public void testFilePayload() throws PayloadNotFoundException {
FilePayload filePayload = new FilePayload(assetFile.getAbsolutePath());
verifyLengthAndRecreation(filePayload, 3381);
verifyLengthAndRecreation(filePayload, assetFile.length());
}

@Test
Expand All @@ -45,9 +46,20 @@ public void testUriPayload() throws PayloadNotFoundException {
}

@Test
public void testBytesPayload() throws PayloadNotFoundException {
ByteArrayPayload byteArrayPayload = new ByteArrayPayload(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
verifyLengthAndRecreation(byteArrayPayload, 10);
public void testBytesPayload() throws PayloadNotFoundException, IOException {
FileInputStream fileInputStream = null;
try {
byte[] buffer = new byte[(int) assetFile.length()];
fileInputStream = new FileInputStream(assetFile);
fileInputStream.read(buffer);
fileInputStream.close();
ByteArrayPayload byteArrayPayload = new ByteArrayPayload(buffer);
verifyLengthAndRecreation(byteArrayPayload, assetFile.length());
} finally {
if (fileInputStream != null) {
fileInputStream.close();
}
}
}

@Test
Expand All @@ -56,7 +68,7 @@ public void testResourcePayload() throws PayloadNotFoundException {
verifyLengthAndRecreation(payload, 3381);
}

private void verifyLengthAndRecreation(Payload payload, int expectedLength) throws PayloadNotFoundException {
private void verifyLengthAndRecreation(Payload payload, long expectedLength) throws PayloadNotFoundException {
assertEquals(expectedLength, payload.getLength(InstrumentationRegistry.getContext()));

String asUri = payload.toUri();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,48 @@
import android.support.annotation.NonNull;
import android.util.Base64;

import com.cloudinary.android.Logger;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
* This class is used to handle uploading of images/videos as byte arrays
*/
public class ByteArrayPayload extends Payload<byte[]> {

public static final String ENCODING_CHARSET = "UTF8";
static final String URI_KEY = "bytes";
private static final String TAG = ByteArrayPayload.class.getSimpleName();

public ByteArrayPayload(byte[] data) {
super(data);
}

public ByteArrayPayload(){
public ByteArrayPayload() {
}

@NonNull
private static String encode(byte[] data) {
return new String(Base64.encode(data, 0));
try {
return new String(Base64.encode(data, Base64.URL_SAFE), ENCODING_CHARSET);
} catch (UnsupportedEncodingException e) {
Logger.e(TAG, "Cannot encode image bytes", e);

// this will be addressed later through the request's flows and callbacks
return null;
}
}

private static byte[] decode(String encoded){
return Base64.decode(encoded, 0);
private static byte[] decode(String encoded) {
try {
return Base64.decode(encoded.getBytes(ENCODING_CHARSET), Base64.URL_SAFE);
} catch (UnsupportedEncodingException e) {
Logger.e(TAG, "Cannot decode image bytes", e);

// this will be addressed later through the request's flows and callbacks
return null;
}
}

@Override
Expand Down