Skip to content

Commit

Permalink
Bug修复: 修复B站弹幕下载乱码问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xyoye committed Apr 14, 2024
1 parent 388b593 commit c1ff89f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.xyoye.common_component.network.config.Api
import com.xyoye.common_component.network.helper.AgentInterceptor
import com.xyoye.common_component.network.helper.AuthInterceptor
import com.xyoye.common_component.network.helper.BackupDomainInterceptor
import com.xyoye.common_component.network.helper.DecompressInterceptor
import com.xyoye.common_component.network.helper.DynamicBaseUrlInterceptor
import com.xyoye.common_component.network.helper.GzipInterceptor
import com.xyoye.common_component.network.helper.LoggerInterceptor
import com.xyoye.common_component.network.service.AlistService
import com.xyoye.common_component.network.service.DanDanService
Expand Down Expand Up @@ -45,7 +45,7 @@ class Retrofit private constructor() {
.hostnameVerifier { _, _ -> true }
.addInterceptor(AgentInterceptor())
.addInterceptor(AuthInterceptor())
.addInterceptor(GzipInterceptor())
.addInterceptor(DecompressInterceptor())
.addInterceptor(BackupDomainInterceptor())
.addInterceptor(LoggerInterceptor().retrofit())
.build()
Expand All @@ -58,6 +58,7 @@ class Retrofit private constructor() {
.writeTimeout(4, TimeUnit.SECONDS)
.hostnameVerifier { _, _ -> true }
.addInterceptor(AgentInterceptor())
.addInterceptor(DecompressInterceptor())
.addInterceptor(DynamicBaseUrlInterceptor())
.addInterceptor(LoggerInterceptor().retrofit())
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.xyoye.common_component.network.helper

import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.Response
import okhttp3.ResponseBody
import okio.BufferedSource
import okio.GzipSource
import okio.InflaterSource
import okio.buffer
import java.util.zip.Inflater

/**
* Created by xyoye on 2024/4/15
*/

class DecompressInterceptor : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val originalResponse = chain.proceed(chain.request())

val originalBody = originalResponse.body
?: return originalResponse
val originalEncoding = originalResponse.header("Content-Encoding")
?: return originalResponse

val decompressEncoding = DecompressEncoding.formEncoding(originalEncoding)
?: return originalResponse

return originalResponse.newBuilder()
.body(DecompressResponseBody(originalBody, decompressEncoding))
.removeHeader("Content-Encoding")
.build()
}

private enum class DecompressEncoding(val encoding: String) {
GZIP("gzip"),

DEFLATE("deflate");

companion object {
fun formEncoding(encoding: String): DecompressEncoding? {
return values().firstOrNull {
it.encoding.equals(encoding, ignoreCase = true)
}
}
}
}

private class DecompressResponseBody(
private val responseBody: ResponseBody,
private val decompressEncoding: DecompressEncoding
) : ResponseBody() {
override fun contentLength(): Long {
return responseBody.contentLength()
}

override fun contentType(): MediaType? {
return responseBody.contentType()
}

override fun source(): BufferedSource {
return when (decompressEncoding) {
DecompressEncoding.DEFLATE -> InflaterSource(responseBody.source(), Inflater(true))
DecompressEncoding.GZIP -> GzipSource(responseBody.source())
}.buffer()
}
}
}

This file was deleted.

This file was deleted.

0 comments on commit c1ff89f

Please sign in to comment.