Skip to content

Commit

Permalink
Fix issue didi#422: end-of-stream caused by multiple collector
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee committed Sep 4, 2023
1 parent ef9f0ad commit 0ecf60f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.didiglobal.booster.transform.util
import com.didiglobal.booster.kotlinx.search
import com.didiglobal.booster.transform.AbstractTransformContext
import com.didiglobal.booster.transform.TransformContext
import org.apache.commons.compress.archivers.ArchiveEntry
import org.apache.commons.compress.archivers.ArchiveStreamFactory
import java.io.File
import java.io.IOException
import java.util.regex.Pattern
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

typealias Collector<T> = com.didiglobal.booster.transform.Collector<T>

Expand Down Expand Up @@ -88,22 +87,17 @@ fun <R> File.collect(collector: Collector<R>): List<R> = when {
collector.collect(base.relativize(f.toURI()).normalize().path, f::readBytes)
}
}
this.isFile -> {
this.inputStream().buffered().use {
ArchiveStreamFactory().createArchiveInputStream(it).let { archive ->
generateSequence {
try {
archive.nextEntry
} catch (e: IOException) {
null
}
}.filterNot(ArchiveEntry::isDirectory).filter { entry ->
collector.accept(entry.name)
}.map { entry ->
collector.collect(entry.name, archive::readBytes)
}.toList()
}
this.isFile -> when (this.extension.lowercase()) {
"jar", "zip" -> ZipFile(this).use { zip ->
zip.entries().asSequence().filterNot(ZipEntry::isDirectory).filter { entry ->
collector.accept(entry.name)
}.map { entry ->
collector.collect(entry.name) {
zip.getInputStream(entry).readBytes()
}
}.toList()
}
else -> emptyList()
}
else -> emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class CompositeCollector(private val collectors: Iterable<Collector<*>>) : Colle
}

override fun collect(name: String, data: () -> ByteArray): List<*> {
val bytes by lazy(data) // to avoid end-of-stream caused by multiple readers
return collectors.filter {
it.accept(name)
}.mapNotNull {
it.collect(name, data)
it.collect(name) { bytes }
}
}

Expand Down

0 comments on commit 0ecf60f

Please sign in to comment.