@Override public long open(DataSpec dataSpec) throws HttpDataSourceException { this.dataSpec = dataSpec; this.bytesRead = 0; this.bytesSkipped = 0; Request request = makeRequest(dataSpec); try { response = okHttpClient.newCall(request).execute(); responseByteStream = response.body().byteStream(); } catch (IOException e) { throw new HttpDataSourceException( "Unable to connect to " + dataSpec.uri.toString(), e, dataSpec); } int responseCode = response.code(); // Check for a valid response code. if (!response.isSuccessful()) { Map<String, List<String>> headers = request.headers().toMultimap(); closeConnectionQuietly(); throw new InvalidResponseCodeException(responseCode, headers, dataSpec); } // Check for a valid content type. MediaType mediaType = response.body().contentType(); String contentType = mediaType != null ? mediaType.toString() : null; if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) { closeConnectionQuietly(); throw new InvalidContentTypeException(contentType, dataSpec); } // If we requested a range starting from a non-zero position and received a 200 rather than a // 206, then the server does not support partial requests. We'll need to manually skip to the // requested position. bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0; // Determine the length of the data to be read, after skipping. long contentLength = response.body().contentLength(); bytesToRead = dataSpec.length != C.LENGTH_UNBOUNDED ? dataSpec.length : contentLength != -1 ? contentLength - bytesToSkip : C.LENGTH_UNBOUNDED; opened = true; if (listener != null) { listener.onTransferStart(); } return bytesToRead; }
@Override public long open(DataSpec dataSpec) throws HttpDataSourceException { this.dataSpec = dataSpec; this.bytesRead = 0; this.bytesSkipped = 0; try { connection = makeConnection(dataSpec); } catch (IOException e) { throw new HttpDataSourceException( "Unable to connect to " + dataSpec.uri.toString(), e, dataSpec); } int responseCode; try { responseCode = connection.getResponseCode(); } catch (IOException e) { closeConnectionQuietly(); throw new HttpDataSourceException( "Unable to connect to " + dataSpec.uri.toString(), e, dataSpec); } // Check for a valid response code. if (responseCode < 200 || responseCode > 299) { Map<String, List<String>> headers = connection.getHeaderFields(); closeConnectionQuietly(); throw new InvalidResponseCodeException(responseCode, headers, dataSpec); } // Check for a valid content type. String contentType = connection.getContentType(); if (contentTypePredicate != null && !contentTypePredicate.evaluate(contentType)) { closeConnectionQuietly(); throw new InvalidContentTypeException(contentType, dataSpec); } // If we requested a range starting from a non-zero position and received a 200 rather than a // 206, then the server does not support partial requests. We'll need to manually skip to the // requested position. bytesToSkip = responseCode == 200 && dataSpec.position != 0 ? dataSpec.position : 0; // Determine the length of the data to be read, after skipping. if ((dataSpec.flags & DataSpec.FLAG_ALLOW_GZIP) == 0) { long contentLength = getContentLength(connection); bytesToRead = dataSpec.length != C.LENGTH_UNBOUNDED ? dataSpec.length : contentLength != C.LENGTH_UNBOUNDED ? contentLength - bytesToSkip : C.LENGTH_UNBOUNDED; } else { // Gzip is enabled. If the server opts to use gzip then the content length in the response // will be that of the compressed data, which isn't what we want. Furthermore, there isn't a // reliable way to determine whether the gzip was used or not. Always use the dataSpec length // in this case. bytesToRead = dataSpec.length; } try { inputStream = connection.getInputStream(); } catch (IOException e) { closeConnectionQuietly(); throw new HttpDataSourceException(e, dataSpec); } opened = true; if (listener != null) { listener.onTransferStart(); } return bytesToRead; }