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? private val myRoot: VirtualFile?
internal val file = File(path) internal val file = File(path)
private val ourEntryMap: Map<String, ZipEntryDescription>
private val cachedManifest: ByteArray? private val cachedManifest: ByteArray?
init { init {
val entries: List<ZipEntryDescription>
RandomAccessFile(file, "r").use { randomAccessFile -> RandomAccessFile(file, "r").use { randomAccessFile ->
val mappedByteBuffer = randomAccessFile.channel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length()) val mappedByteBuffer = randomAccessFile.channel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length())
try { try {
ourEntryMap = mappedByteBuffer.parseCentralDirectory().associateBy { it.relativePath } entries = mappedByteBuffer.parseCentralDirectory()
cachedManifest = ourEntryMap[MANIFEST_PATH]?.let(mappedByteBuffer::contentsToByteArray) cachedManifest = entries.singleOrNull { it.relativePath == MANIFEST_PATH }?.let(mappedByteBuffer::contentsToByteArray)
} finally { } finally {
with(fileSystem) { with(fileSystem) {
mappedByteBuffer.unmapBuffer() 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 filesByRelativePath[""] = myRoot
for (entryDescription in ourEntryMap.values) { for (entryDescription in entries) {
if (!entryDescription.isDirectory) { if (!entryDescription.isDirectory) {
createFile(entryDescription, filesByRelativePath) createFile(entryDescription, filesByRelativePath)
} else { } else {
@@ -59,7 +61,8 @@ class FastJarHandler(val fileSystem: FastJarFileSystem, path: String) {
return FastJarVirtualFile( return FastJarVirtualFile(
this, shortName, this, shortName,
if (entry.isDirectory) -1 else entry.uncompressedSize, 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 (parentPath, shortName) = entryName.splitPath()
val parentFile = getOrCreateDirectory(parentPath, directories) 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) 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") if (relativePath == MANIFEST_PATH) return cachedManifest ?: throw FileNotFoundException("$file!/$relativePath")
val zipEntryDescription = ourEntryMap[relativePath] ?: throw FileNotFoundException("$file!/$relativePath")
return fileSystem.cachedOpenFileHandles[file].use { return fileSystem.cachedOpenFileHandles[file].use {
synchronized(it) { synchronized(it) {
it.get().second.contentsToByteArray(zipEntryDescription) it.get().second.contentsToByteArray(zipEntryDescription)
@@ -4,7 +4,6 @@
*/ */
package org.jetbrains.kotlin.cli.jvm.compiler.jarfs 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.BufferExposingByteArrayInputStream
import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFile
@@ -17,7 +16,8 @@ internal class FastJarVirtualFile(
private val myHandler: FastJarHandler, private val myHandler: FastJarHandler,
private val myName: CharSequence, private val myName: CharSequence,
private val myLength: Int, private val myLength: Int,
private val myParent: FastJarVirtualFile? private val myParent: FastJarVirtualFile?,
private val entryDescription: ZipEntryDescription?,
) : VirtualFile() { ) : VirtualFile() {
private var myChildrenArray = EMPTY_ARRAY private var myChildrenArray = EMPTY_ARRAY
@@ -85,10 +85,8 @@ internal class FastJarVirtualFile(
@Throws(IOException::class) @Throws(IOException::class)
override fun contentsToByteArray(): ByteArray { override fun contentsToByteArray(): ByteArray {
val pair: Couple<String> = FastJarFileSystem.splitPath( if (entryDescription == null) return EMPTY_BYTE_ARRAY
path return myHandler.contentsToByteArray(entryDescription)
)
return myHandler.contentsToByteArray(pair.second)
} }
override fun getTimeStamp(): Long = 0 override fun getTimeStamp(): Long = 0
@@ -106,3 +104,5 @@ internal class FastJarVirtualFile(
return 0 return 0
} }
} }
private val EMPTY_BYTE_ARRAY = ByteArray(0)