-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...omponent/src/main/java/com/xyoye/common_component/network/helper/DecompressInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
29 changes: 0 additions & 29 deletions
29
common_component/src/main/java/com/xyoye/common_component/network/helper/GzipInterceptor.kt
This file was deleted.
Oops, something went wrong.
112 changes: 0 additions & 112 deletions
112
common_component/src/main/java/com/xyoye/common_component/utils/GZIPUtils.kt
This file was deleted.
Oops, something went wrong.