From 7b11bf5bfde879257840ffd842ce7e25df27bfdf Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 16 May 2023 23:07:17 +0200 Subject: [PATCH] CharsetToolkit not required for aalto xml GitOrigin-RevId: e0165ef48d77a60df50e9093311d8b3319ad26cc --- .../src/schemeManager/schemeLoader.kt | 41 +++++++++++-------- .../src/schemeManager/schemeManagerUtil.kt | 2 +- .../testSrc/SchemeManagerTest.kt | 4 +- .../configurationStore/StreamProvider.kt | 7 +++- .../SchemeManagerIprProvider.kt | 11 ++--- .../settingsSync/SettingsSyncTestInfra.kt | 4 +- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/platform/configuration-store-impl/src/schemeManager/schemeLoader.kt b/platform/configuration-store-impl/src/schemeManager/schemeLoader.kt index 8179a6278e86..d09300ede045 100644 --- a/platform/configuration-store-impl/src/schemeManager/schemeLoader.kt +++ b/platform/configuration-store-impl/src/schemeManager/schemeLoader.kt @@ -11,7 +11,6 @@ import com.intellij.openapi.options.NonLazySchemeProcessor import com.intellij.openapi.options.Scheme import com.intellij.openapi.project.ProjectBundle import com.intellij.openapi.util.JDOMUtil -import com.intellij.openapi.vfs.CharsetToolkit import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.containers.ContainerUtil @@ -28,10 +27,10 @@ import java.util.concurrent.atomic.AtomicBoolean import javax.xml.stream.XMLStreamConstants import javax.xml.stream.XMLStreamReader -internal class SchemeLoader(private val schemeManager: SchemeManagerImpl, - private val oldSchemes: List, - private val preScheduledFilesToDelete: MutableSet, - private val isDuringLoad: Boolean) { +internal class SchemeLoader(private val schemeManager: SchemeManagerImpl, + private val oldSchemes: List, + private val preScheduledFilesToDelete: MutableSet, + private val isDuringLoad: Boolean) { private val filesToDelete: MutableSet = HashSet() private val schemes: MutableList = oldSchemes.toMutableList() @@ -58,7 +57,7 @@ internal class SchemeLoader(private val schemeMan */ fun apply(): List { LOG.assertTrue(isApplied.compareAndSet(false, true)) - if (filesToDelete.isNotEmpty() || preScheduledFilesToDelete.isNotEmpty()) { + if (!filesToDelete.isEmpty() || !preScheduledFilesToDelete.isEmpty()) { LOG.debug { "Schedule to delete: ${filesToDelete.joinToString()} (and preScheduledFilesToDelete: ${preScheduledFilesToDelete.joinToString()})" } @@ -66,6 +65,10 @@ internal class SchemeLoader(private val schemeMan schemeManager.filesToDelete.addAll(preScheduledFilesToDelete) } + if (newSchemesOffset == schemes.size) { + return emptyList() + } + val result = schemes.subList(newSchemesOffset, schemes.size) schemeManager.schemeListManager.replaceSchemeList(oldList = oldSchemes, newList = schemes, newSchemeToInfo = schemeToInfo) if (!isDuringLoad) { @@ -161,7 +164,7 @@ internal class SchemeLoader(private val schemeMan var scheme: MUTABLE_SCHEME? = null if (processor is LazySchemeProcessor) { - val bytes = preloadedBytes ?: input!!.readBytes() + val bytes = preloadedBytes ?: input!!.readAllBytes() lazyPreloadScheme(bytes, schemeManager.isOldSchemeNaming) { name, parser -> val attributeProvider: (String) -> String? = { if (parser.eventType == XMLStreamConstants.START_ELEMENT) { @@ -178,20 +181,24 @@ internal class SchemeLoader(private val schemeMan return null } - val externalInfo = createInfo(schemeKey, null) - scheme = processor.createScheme(SchemeDataHolderImpl(processor, bytes, externalInfo), schemeKey, attributeProvider) + val externalInfo = createInfo(schemeName = schemeKey, element = null) + scheme = processor.createScheme( + dataHolder = SchemeDataHolderImpl(processor = processor, bytes = bytes, externalInfo = externalInfo), + name = schemeKey, + attributeProvider = attributeProvider, + ) schemeToInfo.put(scheme!!, externalInfo) retainProbablyScheduledForDeleteFile(fileName) } } else { - val element = when (preloadedBytes) { - null -> JDOMUtil.load(input) - else -> JDOMUtil.load(CharsetToolkit.inputStreamSkippingBOM(preloadedBytes.inputStream())) - } + val element = if (preloadedBytes == null) JDOMUtil.load(input) else JDOMUtil.load(preloadedBytes) scheme = (processor as NonLazySchemeProcessor).readScheme(element, isDuringLoad) ?: return null val schemeKey = processor.getSchemeKey(scheme!!) - if (!checkExisting(schemeKey, fileName, fileNameWithoutExtension, extension)) { + if (!checkExisting(schemeKey = schemeKey, + fileName = fileName, + fileNameWithoutExtension = fileNameWithoutExtension, + extension = extension)) { return null } @@ -221,7 +228,9 @@ internal class SchemeLoader(private val schemeMan } } -internal inline fun lazyPreloadScheme(bytes: ByteArray, isOldSchemeNaming: Boolean, consumer: (name: String?, parser: XMLStreamReader) -> Unit) { +internal inline fun lazyPreloadScheme(bytes: ByteArray, + isOldSchemeNaming: Boolean, + consumer: (name: String?, parser: XMLStreamReader) -> Unit) { val reader = createXmlStreamReader(bytes) consumer(preload(isOldSchemeNaming, reader), reader) } @@ -304,6 +313,6 @@ internal fun createDir(ioDir: Path, requestor: StorageManagerFileWriteRequestor) ioDir.createDirectories() val parentFile = ioDir.parent val parentVirtualFile = (if (parentFile == null) null else VfsUtil.createDirectoryIfMissing(parentFile.systemIndependentPath)) - ?: throw IOException(ProjectBundle.message("project.configuration.save.file.not.found", parentFile)) + ?: throw IOException(ProjectBundle.message("project.configuration.save.file.not.found", parentFile)) return parentVirtualFile.getOrCreateChild(ioDir.fileName.toString(), requestor) } \ No newline at end of file diff --git a/platform/configuration-store-impl/src/schemeManager/schemeManagerUtil.kt b/platform/configuration-store-impl/src/schemeManager/schemeManagerUtil.kt index 248d6306cb13..c93af335361e 100644 --- a/platform/configuration-store-impl/src/schemeManager/schemeManagerUtil.kt +++ b/platform/configuration-store-impl/src/schemeManager/schemeManagerUtil.kt @@ -22,5 +22,5 @@ internal inline fun catchAndLog(file: () -> String, runnable: () -> T): T? { } internal fun nameIsMissed(bytes: ByteArray): RuntimeException { - return RuntimeException("Name is missed:\n${bytes.toString(Charsets.UTF_8)}") + return RuntimeException("Name is missed:\n${bytes.decodeToString()}") } \ No newline at end of file diff --git a/platform/configuration-store-impl/testSrc/SchemeManagerTest.kt b/platform/configuration-store-impl/testSrc/SchemeManagerTest.kt index 030dd7efc7c2..5d3df90973a1 100644 --- a/platform/configuration-store-impl/testSrc/SchemeManagerTest.kt +++ b/platform/configuration-store-impl/testSrc/SchemeManagerTest.kt @@ -18,7 +18,7 @@ import com.intellij.testFramework.* import com.intellij.testFramework.rules.InMemoryFsRule import com.intellij.util.PathUtil import com.intellij.util.io.* -import com.intellij.util.toBufferExposingByteArray +import com.intellij.util.toByteArray import com.intellij.util.xmlb.annotations.Tag import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -143,7 +143,7 @@ internal class SchemeManagerTest { } fun TestScheme.save(file: Path) { - file.write(serialize(this)!!.toBufferExposingByteArray().toByteArray()) + file.write(serialize(this)!!.toByteArray()) } @Test fun `different extensions - old, new`() { diff --git a/platform/projectModel-api/src/com/intellij/configurationStore/StreamProvider.kt b/platform/projectModel-api/src/com/intellij/configurationStore/StreamProvider.kt index 68366f99089b..d937f304a89c 100644 --- a/platform/projectModel-api/src/com/intellij/configurationStore/StreamProvider.kt +++ b/platform/projectModel-api/src/com/intellij/configurationStore/StreamProvider.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.configurationStore import com.intellij.openapi.components.RoamingType @@ -46,7 +46,10 @@ interface StreamProvider { /** * `true` if provider is fully responsible and local sources must be not used. */ - fun processChildren(path: String, roamingType: RoamingType, filter: (name: String) -> Boolean, processor: (name: String, input: InputStream, readOnly: Boolean) -> Boolean): Boolean + fun processChildren(path: String, + roamingType: RoamingType, + filter: (name: String) -> Boolean, + processor: (name: String, input: InputStream, readOnly: Boolean) -> Boolean): Boolean /** * Delete file or directory diff --git a/platform/projectModel-impl/src/com/intellij/configurationStore/SchemeManagerIprProvider.kt b/platform/projectModel-impl/src/com/intellij/configurationStore/SchemeManagerIprProvider.kt index 8ae68f1308e7..1b56eb5904b0 100644 --- a/platform/projectModel-impl/src/com/intellij/configurationStore/SchemeManagerIprProvider.kt +++ b/platform/projectModel-impl/src/com/intellij/configurationStore/SchemeManagerIprProvider.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. @file:Suppress("ReplacePutWithAssignment", "ReplaceGetOrSet") package com.intellij.configurationStore @@ -9,14 +9,15 @@ import com.intellij.openapi.util.SimpleModificationTracker import com.intellij.openapi.util.io.FileUtil import com.intellij.util.PathUtilRt import com.intellij.util.text.UniqueNameGenerator -import com.intellij.util.toBufferExposingByteArray +import com.intellij.util.toByteArray import org.jdom.Element import java.io.InputStream import java.util.concurrent.locks.ReentrantReadWriteLock import kotlin.concurrent.read import kotlin.concurrent.write -class SchemeManagerIprProvider(private val subStateTagName: String, private val comparator: Comparator? = null) : StreamProvider, SimpleModificationTracker() { +class SchemeManagerIprProvider(private val subStateTagName: String, + private val comparator: Comparator? = null) : StreamProvider, SimpleModificationTracker() { private val lock = ReentrantReadWriteLock() private var nameToData = LinkedHashMap() @@ -90,7 +91,7 @@ class SchemeManagerIprProvider(private val subStateTagName: String, private val continue } - nameToData.put(nameGenerator.generateUniqueName("${FileUtil.sanitizeFileName(name, false)}.xml"), child.toBufferExposingByteArray().toByteArray()) + nameToData.put(nameGenerator.generateUniqueName("${FileUtil.sanitizeFileName(name, false)}.xml"), child.toByteArray()) } lock.write { @@ -115,7 +116,7 @@ class SchemeManagerIprProvider(private val subStateTagName: String, private val names.sortWith(comparator) } for (name in names) { - nameToData.get(name)?.let { state.addContent(JDOMUtil.load(it.inputStream())) } + nameToData.get(name)?.let { state.addContent(JDOMUtil.load(it)) } } } } diff --git a/plugins/settings-sync/tests/com/intellij/settingsSync/SettingsSyncTestInfra.kt b/plugins/settings-sync/tests/com/intellij/settingsSync/SettingsSyncTestInfra.kt index 49d1ed9d6ad9..69c02bfc2253 100644 --- a/plugins/settings-sync/tests/com/intellij/settingsSync/SettingsSyncTestInfra.kt +++ b/plugins/settings-sync/tests/com/intellij/settingsSync/SettingsSyncTestInfra.kt @@ -10,7 +10,7 @@ import com.intellij.openapi.extensions.PluginId import com.intellij.settingsSync.SettingsSnapshot.MetaInfo import com.intellij.settingsSync.plugins.SettingsSyncPluginsState import com.intellij.settingsSync.plugins.SettingsSyncPluginsState.PluginData -import com.intellij.util.toBufferExposingByteArray +import com.intellij.util.toByteArray import com.intellij.util.xmlb.Constants import org.jdom.Element import org.jetbrains.annotations.ApiStatus @@ -68,7 +68,7 @@ internal fun PersistentStateComponent<*>.serialize(): ByteArray { val appElement = Element("application") appElement.addContent(compElement) - return appElement.toBufferExposingByteArray().toByteArray() + return appElement.toByteArray() } internal fun settingsSnapshot(metaInfo: MetaInfo = MetaInfo(Instant.now(), getLocalApplicationInfo()),