diff --git a/lib/src/androidTest/java/com/cloudinary/android/PayloadTest.java b/lib/src/androidTest/java/com/cloudinary/android/PayloadTest.java index 4944a65e..fd50524e 100644 --- a/lib/src/androidTest/java/com/cloudinary/android/PayloadTest.java +++ b/lib/src/androidTest/java/com/cloudinary/android/PayloadTest.java @@ -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; @@ -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 @@ -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 @@ -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(); diff --git a/lib/src/main/java/com/cloudinary/android/payload/ByteArrayPayload.java b/lib/src/main/java/com/cloudinary/android/payload/ByteArrayPayload.java index 4108394d..18062d2f 100755 --- a/lib/src/main/java/com/cloudinary/android/payload/ByteArrayPayload.java +++ b/lib/src/main/java/com/cloudinary/android/payload/ByteArrayPayload.java @@ -4,6 +4,9 @@ import android.support.annotation.NonNull; import android.util.Base64; +import com.cloudinary.android.Logger; + +import java.io.UnsupportedEncodingException; import java.util.Arrays; /** @@ -11,22 +14,38 @@ */ public class ByteArrayPayload extends Payload { + 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