提交 e93b0a9c 编写于 作者: S Sam Judd

Close streams in OkHttp and HttpUrl fetchers.

上级 2bd356be
......@@ -6,6 +6,7 @@ import com.bumptech.glide.load.model.GlideUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import java.io.IOException;
import java.io.InputStream;
/**
......@@ -15,6 +16,7 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private final OkHttpClient client;
private final GlideUrl url;
private volatile Request request;
private InputStream stream;
public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) {
this.client = client;
......@@ -27,12 +29,23 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
.url(url.toString())
.build();
return client.newCall(request).execute().body().byteStream();
stream = client.newCall(request)
.execute()
.body()
.byteStream();
return stream;
}
@Override
public void cleanup() {
// Do nothing.
if (stream == null) {
return;
}
try {
stream.close();
} catch (IOException e) {
// Ignored
}
}
@Override
......
......@@ -2,9 +2,9 @@ package com.bumptech.glide.load.data;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.model.GlideUrl;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
......@@ -15,6 +15,7 @@ import java.net.URL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
......@@ -24,16 +25,20 @@ public class HttpUrlFetcherTest {
private HttpURLConnection urlConnection;
private HttpUrlFetcher fetcher;
private GlideUrl glideUrl;
private InputStream stream;
@Before
public void setUp() throws IOException {
urlConnection = mock(HttpURLConnection.class);
URL url = new URL("http://www.google.com");
HttpUrlFetcher.HttpUrlConnectionFactory factory = mock(HttpUrlFetcher.HttpUrlConnectionFactory.class);
when(factory.build(eq(url))).thenReturn(urlConnection);
HttpUrlFetcher.HttpUrlConnectionFactory connectionFactory = mock(HttpUrlFetcher.HttpUrlConnectionFactory.class);
when(connectionFactory.build(eq(url))).thenReturn(urlConnection);
glideUrl = mock(GlideUrl.class);
when(glideUrl.toURL()).thenReturn(url);
fetcher = new HttpUrlFetcher(glideUrl, factory);
fetcher = new HttpUrlFetcher(glideUrl, connectionFactory);
stream = mock(InputStream.class);
when(urlConnection.getInputStream()).thenReturn(stream);
when(urlConnection.getResponseCode()).thenReturn(200);
}
......@@ -90,4 +95,22 @@ public class HttpUrlFetcherTest {
verify(urlConnection, never()).disconnect();
}
@Test
public void testClosesStreamInCleanupIfNotNull() throws Exception {
fetcher.loadData(Priority.HIGH);
fetcher.cleanup();
verify(stream).close();
}
@Test
public void testClosesStreamBeforeDisconnectingConnection() throws Exception {
fetcher.loadData(Priority.NORMAL);
fetcher.cleanup();
InOrder order = inOrder(stream, urlConnection);
order.verify(stream).close();
order.verify(urlConnection).disconnect();
}
}
\ No newline at end of file
......@@ -13,20 +13,23 @@ import java.net.URL;
*/
public class HttpUrlFetcher implements DataFetcher<InputStream> {
private static final int MAXIMUM_REDIRECTS = 5;
private static final HttpUrlConnectionFactory DEFAULT_FACTORY = new DefaultHttpUrlConnectionFactory();
private static final HttpUrlConnectionFactory DEFAULT_CONNECTION_FACTORY = new DefaultHttpUrlConnectionFactory();
private final HttpUrlConnectionFactory factory;
private final GlideUrl glideUrl;
private final HttpUrlConnectionFactory connectionFactory;
private HttpURLConnection urlConnection;
private InputStream stream;
private volatile boolean isCancelled;
public HttpUrlFetcher(GlideUrl glideUrl) {
this(glideUrl, DEFAULT_FACTORY);
this(glideUrl, DEFAULT_CONNECTION_FACTORY);
}
HttpUrlFetcher(GlideUrl glideUrl, HttpUrlConnectionFactory factory) {
// Visible for testing.
HttpUrlFetcher(GlideUrl glideUrl, HttpUrlConnectionFactory connectionFactory) {
this.glideUrl = glideUrl;
this.factory = factory;
this.connectionFactory = connectionFactory;
}
@Override
......@@ -40,7 +43,7 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
} else if (url.equals(lastUrl)) {
throw new IOException("In re-direct loop");
}
urlConnection = factory.build(url);
urlConnection = connectionFactory.build(url);
urlConnection.setConnectTimeout(2500);
urlConnection.setReadTimeout(2500);
urlConnection.setUseCaches(false);
......@@ -53,7 +56,8 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
}
final int statusCode = urlConnection.getResponseCode();
if (statusCode / 100 == 2) {
return urlConnection.getInputStream();
stream = urlConnection.getInputStream();
return stream;
} else if (statusCode / 100 == 3) {
String redirectUrlString = urlConnection.getHeaderField("Location");
URL redirectUrl = new URL(redirectUrlString);
......@@ -68,6 +72,13 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
@Override
public void cleanup() {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignore
}
}
if (urlConnection != null) {
urlConnection.disconnect();
}
......@@ -95,5 +106,4 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
return (HttpURLConnection) url.openConnection();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册