Skip to content

Commit c6ae2f3

Browse files
committed
Various small doc, case-sensitibity & nullability fixes
1 parent 8cddcb4 commit c6ae2f3

1 file changed

Lines changed: 15 additions & 27 deletions

File tree

  • libraries/datasource_ktor/src/main/java/androidx/media3/datasource/ktor

libraries/datasource_ktor/src/main/java/androidx/media3/datasource/ktor/KtorDataSource.kt

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import androidx.media3.datasource.HttpDataSource
2828
import androidx.media3.datasource.HttpUtil
2929
import androidx.media3.datasource.TransferListener
3030
import com.google.common.base.Predicate
31+
import com.google.common.collect.Maps
3132
import com.google.common.net.HttpHeaders
3233
import io.ktor.client.HttpClient
3334
import io.ktor.client.request.headers
@@ -66,7 +67,7 @@ private constructor(
6667
private val defaultRequestProperties: HttpDataSource.RequestProperties?,
6768
private val contentTypePredicate: Predicate<String>?,
6869
private val requestProperties: HttpDataSource.RequestProperties,
69-
) : BaseDataSource(true), HttpDataSource {
70+
) : BaseDataSource(/* isNetwork= */ true), HttpDataSource {
7071

7172
companion object {
7273
private const val TAG = "KtorDataSource"
@@ -186,27 +187,18 @@ private constructor(
186187
private var bytesRead: Long = 0
187188

188189
override fun getUri(): Uri? {
189-
return if (response != null) {
190-
Uri.parse(response!!.request.url.toString())
191-
} else if (dataSpec != null) {
192-
dataSpec!!.uri
193-
} else {
194-
null
195-
}
190+
return this.response?.request?.url?.let { Uri.parse(it.toString()) } ?: this.dataSpec?.uri
196191
}
197192

198193
override fun getResponseCode(): Int {
199194
return response?.status?.value ?: -1
200195
}
201196

202197
override fun getResponseHeaders(): Map<String, List<String>> {
203-
val httpResponse = response ?: return emptyMap()
204-
// ordered map use case-insensitive comparator to support case-insensitive lookup
205-
val result = TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER)
206-
for (name in httpResponse.headers.names()) {
207-
result[name] = httpResponse.headers.getAll(name) ?: emptyList()
198+
val headers = response?.headers ?: return emptyMap()
199+
return Maps.asMap<String, List<String>>(headers.names()) {
200+
name -> headers.getAll(name) as List<String>
208201
}
209-
return result
210202
}
211203

212204
override fun setRequestProperty(name: String, value: String) {
@@ -241,9 +233,9 @@ private constructor(
241233
}
242234

243235
val mergedHeaders = HashMap<String, String>()
244-
defaultRequestProperties?.snapshot?.forEach { (key, value) -> mergedHeaders[key] = value }
245-
requestProperties.snapshot.forEach { (key, value) -> mergedHeaders[key] = value }
246-
dataSpec.httpRequestHeaders.forEach { (key, value) -> mergedHeaders[key] = value }
236+
defaultRequestProperties?.snapshot?.let { properties -> mergedHeaders.putAll(properties) }
237+
mergedHeaders.putAll(requestProperties.snapshot)
238+
mergedHeaders.putAll(dataSpec.httpRequestHeaders)
247239

248240
val httpResponse: HttpResponse
249241
val channel: ByteReadChannel
@@ -255,24 +247,20 @@ private constructor(
255247
url(urlString)
256248

257249
headers {
258-
mergedHeaders.forEach { (key, value) -> append(key, value) }
250+
mergedHeaders.forEach { (key, value) -> set(key, value) }
259251

260252
val rangeHeader =
261253
HttpUtil.buildRangeRequestHeader(dataSpec.position, dataSpec.length)
262-
if (rangeHeader != null) {
263-
append(HttpHeaders.RANGE, rangeHeader)
264-
}
254+
rangeHeader?.let { set(HttpHeaders.RANGE, rangeHeader) }
265255

266-
if (userAgent != null) {
267-
append(HttpHeaders.USER_AGENT, userAgent)
268-
}
256+
userAgent?.let { set(HttpHeaders.USER_AGENT, userAgent) }
269257

270-
if (cacheControl != null) {
271-
append(HttpHeaders.CACHE_CONTROL, cacheControl)
258+
cacheControl?.let {
259+
set(HttpHeaders.CACHE_CONTROL, cacheControl)
272260
}
273261

274262
if (!dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP)) {
275-
append(HttpHeaders.ACCEPT_ENCODING, "identity")
263+
set(HttpHeaders.ACCEPT_ENCODING, "identity")
276264
}
277265
}
278266

0 commit comments

Comments
 (0)