-
Notifications
You must be signed in to change notification settings - Fork 207
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
Perf: use buffered streams for IO #307
Changes from 6 commits
7be488e
986f93d
e00ff45
4969511
8564c42
0c11014
7e57e92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.bugsnag.android; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class JsonWriterTest { | ||
|
||
@Test(expected = IOException.class) | ||
public void testClose() throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
OutputStreamWriter writer = new OutputStreamWriter(baos); | ||
JsonWriter jsonWriter = new JsonWriter(writer); | ||
|
||
jsonWriter.beginObject().endObject(); | ||
jsonWriter.flush(); | ||
assertEquals("{}", new String(baos.toByteArray(), "UTF-8")); | ||
|
||
jsonWriter.close(); | ||
writer.write(5); // can't write to a closed stream, throws IOException | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,11 @@ | |
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.FileWriter; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
@@ -83,23 +86,21 @@ String write(@NonNull T streamable) { | |
|
||
String filename = getFilename(streamable); | ||
|
||
Writer out = null; | ||
JsonStream stream = null; | ||
lock.lock(); | ||
|
||
try { | ||
out = new FileWriter(filename); | ||
|
||
JsonStream stream = new JsonStream(out); | ||
FileOutputStream fos = new FileOutputStream(filename); | ||
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")); | ||
stream = new JsonStream(out); | ||
stream.value(streamable); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR but should we be closing this stream on the next line in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that would make more sense than closing two separate streams. I've updated the PR so that we do this. |
||
stream.close(); | ||
|
||
Logger.info(String.format("Saved unsent payload to disk (%s) ", filename)); | ||
return filename; | ||
} catch (Exception exception) { | ||
Logger.warn(String.format("Couldn't save unsent payload to disk (%s) ", | ||
filename), exception); | ||
} finally { | ||
IOUtils.closeQuietly(out); | ||
IOUtils.closeQuietly(stream); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Q as above. |
||
lock.unlock(); | ||
} | ||
return null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,12 @@ | |
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
|
||
public class JsonStream extends JsonWriter { | ||
|
@@ -57,9 +60,10 @@ public void value(@NonNull File file) throws IOException { | |
beforeValue(false); // add comma if in array | ||
|
||
// Copy the file contents onto the stream | ||
FileReader input = null; | ||
Reader input = null; | ||
try { | ||
input = new FileReader(file); | ||
FileInputStream fis = new FileInputStream(file); | ||
input = new BufferedReader(new InputStreamReader(fis, "UTF-8")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do any of these objects also need closing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe calling |
||
IOUtils.copy(input, out); | ||
} finally { | ||
IOUtils.closeQuietly(input); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will closing the
JsonStream
close theOutputStream
as well? There's no explicit closing of theOutputStream
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - also answered in previous comments in a bit more depth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically: https://github.com/bugsnag/bugsnag-android/blob/master/sdk/src/main/java/com/bugsnag/android/JsonWriter.java#L423
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, is this verifiable or testable at all? It seems like something that could change across implementations/versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a test to check that JsonWriter closes its underlying stream - beyond that we'd just be testing the behaviour of the Java standard library.