Skip to content

Commit

Permalink
Merge pull request joelittlejohn#7 from flibbertigibbet/feature/flush…
Browse files Browse the repository at this point in the history
…-output-stream

Flush output stream after serializing media
  • Loading branch information
flibbertigibbet committed Feb 6, 2016
2 parents 5a2c003 + 1369608 commit 81ecfaf
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public void write(JsonWriter writer, SerializableMedia value) throws IOException
return;
}

// attempt to read in image at path into data
// attempt to read in image at path into data field
boolean readFromPath = false;
if (value.data == null && value.path != null && !value.path.isEmpty()) {
readFromPath = true;
File file = new File(value.path);
value.data = FileUtils.readFileToByteArray(file);
}
Expand All @@ -81,6 +83,13 @@ public void write(JsonWriter writer, SerializableMedia value) throws IOException

// prepend byte string with URI scheme, like: data:image/jpeg;base64,
writer.value("data:" + mimeType + ";base64," + Base64.encodeToString(value.data, Base64.NO_WRAP));

writer.flush(); // force an output stream flush after serializing media

// if bytes were read in from file for this serialization, clear them back out when done
if (readFromPath) {
value.data = null;
}
} else {
writer.nullValue();
}
Expand Down

0 comments on commit 81ecfaf

Please sign in to comment.