Skip to content

Commit 97d7ae7

Browse files
committed
remove StringBuffer on MD5 digest.
add cache tips.
1 parent 96bc56d commit 97d7ae7

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

library/src/main/java/com/opensource/svgaplayer/SVGAParser.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package com.opensource.svgaplayer
22

33
import android.app.Activity
44
import android.content.Context
5+
import android.net.http.HttpResponseCache
56
import android.os.Handler
7+
import android.util.Log
68
import com.opensource.svgaplayer.proto.MovieEntity
79

810
import org.json.JSONObject
@@ -31,9 +33,15 @@ class SVGAParser(private val context: Context) {
3133

3234
open class FileDownloader {
3335

36+
var noCache = false
37+
3438
open fun resume(url: URL, complete: (inputStream: InputStream) -> Unit, failure: (e: Exception) -> Unit) {
3539
Thread({
3640
try {
41+
if (HttpResponseCache.getInstalled() == null && !noCache) {
42+
Log.e("SVGAParser", "SVGAParser can not handle cache before install HttpResponseCache. see https://github.com/yyued/SVGAPlayer-Android#cache")
43+
Log.e("SVGAParser", "在配置 HttpResponseCache 前 SVGAParser 无法缓存. 查看 https://github.com/yyued/SVGAPlayer-Android#cache ")
44+
}
3745
(url.openConnection() as? HttpURLConnection)?.let {
3846
it.connectTimeout = 20 * 1000
3947
it.requestMethod = "GET"
@@ -226,20 +234,16 @@ class SVGAParser(private val context: Context) {
226234
val messageDigest = MessageDigest.getInstance("MD5")
227235
messageDigest.update(str.toByteArray(charset("UTF-8")))
228236
val digest = messageDigest.digest()
229-
val sb = StringBuffer()
237+
var sb = ""
230238
for (b in digest) {
231-
sb.append(String.format("%02x", b))
239+
sb += String.format("%02x", b)
232240
}
233-
return sb.toString()
241+
return sb
234242
}
235243

236-
private fun cacheKey(url: URL): String {
237-
return cacheKey(url.toString())
238-
}
244+
private fun cacheKey(url: URL): String = cacheKey(url.toString())
239245

240-
private fun cacheDir(cacheKey: String): File {
241-
return File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
242-
}
246+
private fun cacheDir(cacheKey: String): File = File(context.cacheDir.absolutePath + "/" + cacheKey + "/")
243247

244248
private fun readAsBytes(inputStream: InputStream): ByteArray {
245249
val byteArrayOutputStream = ByteArrayOutputStream()

0 commit comments

Comments
 (0)