@@ -21,6 +21,7 @@ import java.util.zip.ZipInputStream
2121 */
2222
2323private var fileLock: Int = 0
24+ private var isUnzipping = false
2425
2526class SVGAParser (context : Context ? ) {
2627 private var mContextRef = WeakReference <Context ?>(context)
@@ -135,14 +136,19 @@ class SVGAParser(context: Context?) {
135136 try {
136137 readAsBytes(inputStream)?.let { bytes ->
137138 if (bytes.size > 4 && bytes[0 ].toInt() == 80 && bytes[1 ].toInt() == 75 && bytes[2 ].toInt() == 3 && bytes[3 ].toInt() == 4 ) {
138- if (! buildCacheDir(cacheKey).exists()) {
139- ByteArrayInputStream (bytes).use {
140- unzip(it, cacheKey)
139+ if (! buildCacheDir(cacheKey).exists() || isUnzipping) {
140+ synchronized(fileLock) {
141+ if (! buildCacheDir(cacheKey).exists()) {
142+ isUnzipping = true
143+ ByteArrayInputStream (bytes).use {
144+ unzip(it, cacheKey)
145+ isUnzipping = false
146+ }
147+ }
141148 }
142149 }
143150 this .decodeFromCacheKey(cacheKey, callback)
144- }
145- else {
151+ } else {
146152 inflate(bytes)?.let {
147153 val videoItem = SVGAVideoEntity (MovieEntity .ADAPTER .decode(it), File (cacheKey))
148154 videoItem.prepare {
@@ -306,36 +312,34 @@ class SVGAParser(context: Context?) {
306312 }
307313
308314 private fun unzip (inputStream : InputStream , cacheKey : String ) {
309- synchronized(fileLock) {
310- val cacheDir = this .buildCacheDir(cacheKey)
311- cacheDir.mkdirs()
312- try {
313- BufferedInputStream (inputStream).use {
314- ZipInputStream (it).use { zipInputStream ->
315- while (true ) {
316- val zipItem = zipInputStream.nextEntry ? : break
317- if (zipItem.name.contains(" /" )) {
318- continue
319- }
320- val file = File (cacheDir, zipItem.name)
321- FileOutputStream (file).use { fileOutputStream ->
322- val buff = ByteArray (2048 )
323- while (true ) {
324- val readBytes = zipInputStream.read(buff)
325- if (readBytes <= 0 ) {
326- break
327- }
328- fileOutputStream.write(buff, 0 , readBytes)
315+ val cacheDir = this .buildCacheDir(cacheKey)
316+ cacheDir.mkdirs()
317+ try {
318+ BufferedInputStream (inputStream).use {
319+ ZipInputStream (it).use { zipInputStream ->
320+ while (true ) {
321+ val zipItem = zipInputStream.nextEntry ? : break
322+ if (zipItem.name.contains(" /" )) {
323+ continue
324+ }
325+ val file = File (cacheDir, zipItem.name)
326+ FileOutputStream (file).use { fileOutputStream ->
327+ val buff = ByteArray (2048 )
328+ while (true ) {
329+ val readBytes = zipInputStream.read(buff)
330+ if (readBytes <= 0 ) {
331+ break
329332 }
333+ fileOutputStream.write(buff, 0 , readBytes)
330334 }
331- zipInputStream.closeEntry()
332335 }
336+ zipInputStream.closeEntry()
333337 }
334338 }
335- } catch (e: Exception ) {
336- cacheDir.delete()
337- throw e
338339 }
340+ } catch (e: Exception ) {
341+ cacheDir.delete()
342+ throw e
339343 }
340344 }
341345}
0 commit comments