Skip to content

Commit

Permalink
closed resource leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbluesky committed Apr 28, 2019
1 parent 1a4432c commit 40aae6a
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions app/src/main/java/com/antony/muzei/pixiv/PixivArtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ private Response sendGetRequest(String url) throws IOException
.addHeader("Referer", PixivArtProviderDefines.PIXIV_HOST)
.url(url);

Response response = httpClient.newCall(builder.build()).execute();
return response;
return httpClient.newCall(builder.build()).execute();
}

private Uri downloadFile(Response response, String token)
{
Context context = getContext();
File downloadedFile = new File(context.getExternalCacheDir(), token + ".png");
FileOutputStream fileStream = null;
InputStream inputStream = null;
try
{
fileStream = new FileOutputStream(downloadedFile);
final InputStream inputStream = response.body().byteStream();
inputStream = response.body().byteStream();
final byte[] buffer = new byte[1024 * 50];
int read;
while ((read = inputStream.read(buffer)) > 0)
Expand All @@ -113,6 +113,9 @@ private Uri downloadFile(Response response, String token)
} catch (IOException ex)
{
return null;
} finally
{
response.body().close();
}

return Uri.fromFile(downloadedFile);
Expand Down Expand Up @@ -167,10 +170,6 @@ protected void onLoadRequested(boolean initial)
overallJson = new JSONObject((rankingResponse.body().string()));
contents = overallJson.getJSONArray("contents");

for(int i = 0; i < LIMIT; i++)
{

}
Random random = new Random();
int cursor = random.nextInt(contents.length());
pic0Meta = contents.getJSONObject(cursor);
Expand All @@ -179,8 +178,6 @@ protected void onLoadRequested(boolean initial)
byline = pic0Meta.getString("user_name");
token = pic0Meta.getString("illust_id");
thumbUri = pic0Meta.getString(("url"));
Log.i(LOG_TAG, title);
Log.i(LOG_TAG, token);
} catch (IOException | JSONException ex)
{
Log.d(LOG_TAG, "error");
Expand All @@ -198,13 +195,11 @@ protected void onLoadRequested(boolean initial)

Uri finalUri = downloadFile(response, token);

Log.i(LOG_TAG, finalUri.toString());

setArtwork(new Artwork.Builder()
addArtwork(new Artwork.Builder()
.title(title)
.byline(byline)
.token(token)
.persistentUri(finalUri)
.token(token)
.webUri(Uri.parse(webUri))
.build());
}
Expand Down

0 comments on commit 40aae6a

Please sign in to comment.