Close DefaultHttpDataSource connections quietly.

This commit is contained in:
Oliver Woodman 2015-09-01 14:02:16 +01:00
parent 10badcc430
commit b3ba7e16c1

View File

@ -59,7 +59,7 @@ public class DefaultHttpDataSource implements HttpDataSource {
public static final int DEFAULT_READ_TIMEOUT_MILLIS = 8 * 1000; public static final int DEFAULT_READ_TIMEOUT_MILLIS = 8 * 1000;
private static final int MAX_REDIRECTS = 20; // Same limit as okhttp. private static final int MAX_REDIRECTS = 20; // Same limit as okhttp.
private static final String TAG = "HttpDataSource"; private static final String TAG = "DefaultHttpDataSource";
private static final Pattern CONTENT_RANGE_HEADER = private static final Pattern CONTENT_RANGE_HEADER =
Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$"); Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$");
private static final AtomicReference<byte[]> skipBufferReference = new AtomicReference<>(); private static final AtomicReference<byte[]> skipBufferReference = new AtomicReference<>();
@ -198,7 +198,7 @@ public class DefaultHttpDataSource implements HttpDataSource {
try { try {
responseCode = connection.getResponseCode(); responseCode = connection.getResponseCode();
} catch (IOException e) { } catch (IOException e) {
closeConnection(); closeConnectionQuietly();
throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e, throw new HttpDataSourceException("Unable to connect to " + dataSpec.uri.toString(), e,
dataSpec); dataSpec);
} }
@ -206,14 +206,14 @@ public class DefaultHttpDataSource implements HttpDataSource {
// Check for a valid response code. // Check for a valid response code.
if (responseCode < 200 || responseCode > 299) { if (responseCode < 200 || responseCode > 299) {
Map<String, List<String>> headers = connection.getHeaderFields(); Map<String, List<String>> headers = connection.getHeaderFields();
closeConnection(); closeConnectionQuietly();
throw new InvalidResponseCodeException(responseCode, headers, dataSpec); throw new InvalidResponseCodeException(responseCode, headers, dataSpec);
} }
// Check for a valid content type. // Check for a valid content type.
String contentType = connection.getContentType(); String contentType = connection.getContentType();
if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) { if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) {
closeConnection(); closeConnectionQuietly();
throw new InvalidContentTypeException(contentType, dataSpec); throw new InvalidContentTypeException(contentType, dataSpec);
} }
@ -239,7 +239,7 @@ public class DefaultHttpDataSource implements HttpDataSource {
try { try {
inputStream = connection.getInputStream(); inputStream = connection.getInputStream();
} catch (IOException e) { } catch (IOException e) {
closeConnection(); closeConnectionQuietly();
throw new HttpDataSourceException(e, dataSpec); throw new HttpDataSourceException(e, dataSpec);
} }
@ -274,7 +274,7 @@ public class DefaultHttpDataSource implements HttpDataSource {
} }
} finally { } finally {
inputStream = null; inputStream = null;
closeConnection(); closeConnectionQuietly();
if (opened) { if (opened) {
opened = false; opened = false;
if (listener != null) { if (listener != null) {
@ -566,11 +566,15 @@ public class DefaultHttpDataSource implements HttpDataSource {
} }
/** /**
* Closes the current connection, if there is one. * Closes the current connection quietly, if there is one.
*/ */
private void closeConnection() { private void closeConnectionQuietly() {
if (connection != null) { if (connection != null) {
connection.disconnect(); try {
connection.disconnect();
} catch (Exception e) {
Log.e(TAG, "Unexpected error while disconnecting", e);
}
connection = null; connection = null;
} }
} }