DraconPersistentStateComponent.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.jonathanxd.dracon.cache.persist
import com.github.jonathanxd.dracon.log.PijulLogEntry
import com.intellij.openapi.components.*
import com.intellij.util.xmlb.XmlSerializerUtil
import java.io.*
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
import java.util.*
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
@State(name = "DraconLogEntries", storages = [Storage(value = StoragePathMacros.CACHE_FILE)], reloadable = true)
@Service(Service.Level.PROJECT)
class DraconPersistentStateComponent() : PersistentStateComponent<DraconPersistentStateComponent> {
public var data: String? = null
override fun getState() = this
override fun loadState(state: DraconPersistentStateComponent) {
XmlSerializerUtil.copyBean(state, this);
}
}
fun String.toPijulLogEntries(): List<PijulLogEntry> {
val decoded = Base64.getDecoder().decode(this)
val reader = ObjectInputStream(ByteArrayInputStream(decoded))
return reader.readObject() as List<PijulLogEntry>
}
fun createLogEntriesPersist(entries: List<PijulLogEntry>): String {
val stream = ByteArrayOutputStream()
val writer = ObjectOutputStream(stream)
writer.writeObject(entries)
writer.close()
return Base64.getEncoder().encodeToString(stream.toByteArray())
}
fun Path.toPijulLogEntriesMap(): Map<String, PijulLogEntry> {
Files.newInputStream(this).use { stream ->
GZIPInputStream(stream).use { gz ->
ObjectInputStream(gz).use { reader ->
return reader.readObject() as Map<String, PijulLogEntry>
}
}
}
}
fun String.toPijulLogEntriesMap(): Map<String, PijulLogEntry> {
val decoded = Base64.getDecoder().decode(this)
val reader = ObjectInputStream(GZIPInputStream(ByteArrayInputStream(decoded)))
return reader.readObject() as Map<String, PijulLogEntry>
}
fun Path.createLogEntriesPersist(entries: Map<String, PijulLogEntry>) {
Files.newOutputStream(this, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING).use { writer ->
GZIPOutputStream(writer).use { compress ->
ObjectOutputStream(compress).use {oos ->
oos.writeObject(entries)
}
}
}
}