Skip to content
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

Merged
merged 7 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sdk/src/main/java/com/bugsnag/android/DefaultHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

class DefaultHttpClient implements ErrorReportApiClient, SessionTrackingApiClient {
Expand Down Expand Up @@ -69,7 +71,9 @@ private int makeRequest(String urlString,

try {
out = conn.getOutputStream();
JsonStream stream = new JsonStream(new OutputStreamWriter(out));
Charset charset = Charset.forName("UTF-8");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, charset));
JsonStream stream = new JsonStream(writer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do any of these other objects need to be closed?

streamable.toStream(stream);
stream.close();
} finally {
Expand Down
6 changes: 5 additions & 1 deletion sdk/src/main/java/com/bugsnag/android/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,7 +72,8 @@ String write(@NonNull T streamable) {

Writer out = null;
try {
out = new FileWriter(filename);
FileOutputStream fos = new FileOutputStream(filename);
out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do any of these objects also need closing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by other inline comment on JsonStream


JsonStream stream = new JsonStream(out);
stream.value(streamable);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 finally block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Expand Down
10 changes: 7 additions & 3 deletions sdk/src/main/java/com/bugsnag/android/JsonStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,9 +56,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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do any of these objects also need closing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe calling BufferedReader#close also closes the InputStreamReader, which in turn closes the FileInputStream, which means that everything is automatically closed.

IOUtils.copy(input, out);
} finally {
IOUtils.closeQuietly(input);
Expand Down