Omit all line terminators for ImageStore.getBase64ForTag

Summary:
FIX #11142

This fixes #11142 and supersedes #11155 as I was unsure how to add/change commits in that PR.

Wrote up a bunch of unit tests for the ImageStore module. The added tests showed that there was indeed a problem with the flags used for the Base64OutputStream, and they also show that that has been fixed now.
Closes https://github.com/facebook/react-native/pull/13856

Differential Revision: D6017764

Pulled By: shergin

fbshipit-source-id: adf667dc722ddfe31449afd8cd20a0a192eacff6
This commit is contained in:
Janis Peisenieks
2017-10-09 22:02:56 -07:00
committed by Facebook Github Bot
parent 7c89cf37c6
commit 7a7bdeec3e
3 changed files with 104 additions and 9 deletions

View File

@@ -78,20 +78,12 @@ public class ImageStoreManager extends ReactContextBaseJavaModule {
ContentResolver contentResolver = getReactApplicationContext().getContentResolver();
Uri uri = Uri.parse(mUri);
InputStream is = contentResolver.openInputStream(uri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Base64OutputStream b64os = new Base64OutputStream(baos, Base64.DEFAULT);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
try {
while ((bytesRead = is.read(buffer)) > -1) {
b64os.write(buffer, 0, bytesRead);
}
mSuccess.invoke(baos.toString());
mSuccess.invoke(convertInputStreamToBase64OutputStream(is));
} catch (IOException e) {
mError.invoke(e.getMessage());
} finally {
closeQuietly(is);
closeQuietly(b64os); // this also closes baos
}
} catch (FileNotFoundException e) {
mError.invoke(e.getMessage());
@@ -99,6 +91,21 @@ public class ImageStoreManager extends ReactContextBaseJavaModule {
}
}
String convertInputStreamToBase64OutputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Base64OutputStream b64os = new Base64OutputStream(baos, Base64.NO_WRAP);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
try {
while ((bytesRead = is.read(buffer)) > -1) {
b64os.write(buffer, 0, bytesRead);
}
} finally {
closeQuietly(b64os); // this also closes baos and flushes the final content to it
}
return baos.toString();
}
private static void closeQuietly(Closeable closeable) {
try {
closeable.close();