Skip to content

Commit

Permalink
autoclose inputstream when base64 image file (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
someok authored and ivpusic committed Jun 18, 2019
1 parent 2f6ccb0 commit 334ca14
Showing 1 changed file with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,30 +401,29 @@ public void openCropper(final ReadableMap options, final Promise promise) {
}

private String getBase64StringFromFile(String absoluteFilePath) {
InputStream inputStream;
try (
InputStream inputStream = new FileInputStream(new File(absoluteFilePath));
ByteArrayOutputStream output = new ByteArrayOutputStream()
) {
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;

try {
inputStream = new FileInputStream(new File(absoluteFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}

byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();

try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}

bytes = output.toByteArray();
return Base64.encodeToString(bytes, Base64.NO_WRAP);
} catch (IOException e) {
e.printStackTrace();
return null;
}

bytes = output.toByteArray();
return Base64.encodeToString(bytes, Base64.NO_WRAP);
}

private String getMimeType(String url) {
Expand Down

0 comments on commit 334ca14

Please sign in to comment.