mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -07:00
Reduce time spent in JAR/ZIP assembly.
Locally, this reduces a run of kobaltw assemble --noIncremental on the Kobalt codebase itself from 47 seconds to 25 seconds. When JarInputStream.nextEntry is invoked, the stream sets its position and length so that it can directly be used to read the underlying content associated with the entry. This avoids the need to call JarFile(localFile).getInputStream(entry) and the cost associated with it. addEntry has a slight change of semantics due to this change - it is now the caller's responsibility to close the associated input stream. The two existing calls to the method were adjusted accordingly.
This commit is contained in:
parent
ebf272bf98
commit
713faa7a3f
1 changed files with 11 additions and 19 deletions
|
@ -53,20 +53,22 @@ class JarUtils {
|
||||||
} else {
|
} else {
|
||||||
if (file.expandJarFiles && foundFile.name.endsWith(".jar") && ! file.from.contains("resources")) {
|
if (file.expandJarFiles && foundFile.name.endsWith(".jar") && ! file.from.contains("resources")) {
|
||||||
kobaltLog(2, " Writing contents of jar file $foundFile")
|
kobaltLog(2, " Writing contents of jar file $foundFile")
|
||||||
val stream = JarInputStream(FileInputStream(localFile))
|
JarInputStream(FileInputStream(localFile)).use { stream ->
|
||||||
var entry = stream.nextEntry
|
var entry = stream.nextEntry
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
if (!entry.isDirectory && !KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) {
|
if (!entry.isDirectory && !KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) {
|
||||||
val ins = JarFile(localFile).getInputStream(entry)
|
addEntry(stream, JarEntry(entry), outputStream, onError)
|
||||||
addEntry(ins, JarEntry(entry), outputStream, onError)
|
|
||||||
}
|
}
|
||||||
entry = stream.nextEntry
|
entry = stream.nextEntry
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
val entryFileName = KFiles.fixSlashes(file.to(foundFile.path))
|
val entryFileName = KFiles.fixSlashes(file.to(foundFile.path))
|
||||||
val entry = JarEntry(entryFileName)
|
val entry = JarEntry(entryFileName)
|
||||||
entry.time = localFile.lastModified()
|
entry.time = localFile.lastModified()
|
||||||
addEntry(FileInputStream(localFile), entry, outputStream, onError)
|
FileInputStream(localFile).use { stream ->
|
||||||
|
addEntry(stream, entry, outputStream, onError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,22 +76,12 @@ class JarUtils {
|
||||||
|
|
||||||
private fun addEntry(inputStream: InputStream, entry: ZipEntry, outputStream: ZipOutputStream,
|
private fun addEntry(inputStream: InputStream, entry: ZipEntry, outputStream: ZipOutputStream,
|
||||||
onError: (Exception) -> Unit = DEFAULT_HANDLER) {
|
onError: (Exception) -> Unit = DEFAULT_HANDLER) {
|
||||||
var bis: BufferedInputStream? = null
|
|
||||||
try {
|
try {
|
||||||
outputStream.putNextEntry(entry)
|
outputStream.putNextEntry(entry)
|
||||||
bis = BufferedInputStream(inputStream)
|
inputStream.copyTo(outputStream, 50 * 1024)
|
||||||
|
|
||||||
val buffer = ByteArray(50 * 1024)
|
|
||||||
while (true) {
|
|
||||||
val count = bis.read(buffer)
|
|
||||||
if (count == -1) break
|
|
||||||
outputStream.write(buffer, 0, count)
|
|
||||||
}
|
|
||||||
outputStream.closeEntry()
|
outputStream.closeEntry()
|
||||||
} catch(ex: Exception) {
|
} catch(ex: Exception) {
|
||||||
onError(ex)
|
onError(ex)
|
||||||
} finally {
|
|
||||||
bis?.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue