Get rid of last map in FastJarHandler

This commit is contained in:
Denis.Zharkov
2021-08-02 21:23:24 +03:00
committed by teamcityserver
parent d46f7738c6
commit f81f28569f
2 changed files with 19 additions and 16 deletions
@@ -14,15 +14,15 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) {
private val myRoot: VirtualFile?
internal val file = File(path)
private val ourEntryMap: Map<String, ZipEntryDescription>
private val cachedManifest: ByteArray?
init {
val entries: List<ZipEntryDescription>
RandomAccessFile(file, "r").use { randomAccessFile ->
val mappedByteBuffer = randomAccessFile.channel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length())
try {
ourEntryMap = mappedByteBuffer.parseCentralDirectory().associateBy { it.relativePath }
cachedManifest = ourEntryMap[MANIFEST_PATH]?.let(mappedByteBuffer::contentsToByteArray)
entries = mappedByteBuffer.parseCentralDirectory()
cachedManifest = entries.singleOrNull { it.relativePath == MANIFEST_PATH }?.let(mappedByteBuffer::contentsToByteArray)
} finally {
with(fileSystem) {
mappedByteBuffer.unmapBuffer()
@@ -30,12 +30,14 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) {
}
}
myRoot = FastJarVirtualFile(this, "", -1, null)
myRoot = FastJarVirtualFile(this, "", -1, myParent = null, entryDescription = null)
val filesByRelativePath = HashMap<String, FastJarVirtualFile>(ourEntryMap.size)
// ByteArrayCharSequence should not be used instead of String
// because the former class does not support equals/hashCode properly
val filesByRelativePath = HashMap<String, FastJarVirtualFile>(entries.size)
filesByRelativePath[""] = myRoot
for (entryDescription in ourEntryMap.values) {
for (entryDescription in entries) {
if (!entryDescription.isDirectory) {
createFile(entryDescription, filesByRelativePath)
} else {
@@ -59,7 +61,8 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) {
return FastJarVirtualFile(
this, shortName,
if (entry.isDirectory) -1 else entry.uncompressedSize,
parentFile
parentFile,
entry,
)
}
@@ -68,7 +71,7 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) {
val (parentPath, shortName) = entryName.splitPath()
val parentFile = getOrCreateDirectory(parentPath, directories)
FastJarVirtualFile(this, shortName, -1, parentFile)
FastJarVirtualFile(this, shortName, -1, parentFile, entryDescription = null)
}
}
@@ -87,9 +90,9 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) {
return myRoot?.findFileByRelativePath(pathInJar)
}
fun contentsToByteArray(relativePath: String): ByteArray {
fun contentsToByteArray(zipEntryDescription: ZipEntryDescription): ByteArray {
val relativePath = zipEntryDescription.relativePath
if (relativePath == MANIFEST_PATH) return cachedManifest ?: throw FileNotFoundException("$file!/$relativePath")
val zipEntryDescription = ourEntryMap[relativePath] ?: throw FileNotFoundException("$file!/$relativePath")
return fileSystem.cachedOpenFileHandles[file].use {
synchronized(it) {
it.get().second.contentsToByteArray(zipEntryDescription)
@@ -4,7 +4,6 @@
*/
package org.jetbrains.kotlin.cli.jvm.compiler.jarfs
import com.intellij.openapi.util.Couple
import com.intellij.openapi.util.io.BufferExposingByteArrayInputStream
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VirtualFile
@@ -17,7 +16,8 @@ internal class FastJarVirtualFile(
private val myHandler: FastJarHandler,
private val myName: CharSequence,
private val myLength: Int,
private val myParent: FastJarVirtualFile?
private val myParent: FastJarVirtualFile?,
private val entryDescription: ZipEntryDescription?,
) : VirtualFile() {
private var myChildrenArray = EMPTY_ARRAY
@@ -85,10 +85,8 @@ internal class FastJarVirtualFile(
@Throws(IOException::class)
override fun contentsToByteArray(): ByteArray {
val pair: Couple<String> = FastJarFileSystem.splitPath(
path
)
return myHandler.contentsToByteArray(pair.second)
if (entryDescription == null) return EMPTY_BYTE_ARRAY
return myHandler.contentsToByteArray(entryDescription)
}
override fun getTimeStamp(): Long = 0
@@ -106,3 +104,5 @@ internal class FastJarVirtualFile(
return 0
}
}
private val EMPTY_BYTE_ARRAY = ByteArray(0)