<commit_before>package com.intellij.space.plugins.pipelines.services

import circlet.pipelines.config.api.ScriptConfig
import circlet.pipelines.config.api.parseProjectConfig
import circlet.pipelines.config.api.printJson
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.getProjectCachePath
import com.intellij.util.io.safeOutputStream
import libraries.io.channels.readUTF8Line
import libraries.klogging.logger
import java.io.IOException
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes

private val log = logger<ScriptKtsPersistentState>()

class ScriptKtsPersistentState(val project: Project) {

  // not for load failure
  fun load(): ScriptConfig? {

    val path = getCacheFile(project)

    val fileAttributes = try {
      Files.readAttributes(path, BasicFileAttributes::class.java)
    }
    catch (ignored: FileSystemException) {
      return null
    }

    if (fileAttributes == null || !fileAttributes.isRegularFile) {
      return null
    }

    val channel = try {
      Files.newByteChannel(path, setOf(StandardOpenOption.READ))
    }
    catch (e: NoSuchFileException) {
      return null
    }
    catch (e: IOException) {
      log.error(e)
      return null
    }

    channel.use {
      return channel.readUTF8Line().parseProjectConfig()
    }
  }

  private fun getCacheFile(project: Project): Path {
    return project.getProjectCachePath("space_automation").resolve(".space.kts.dat")
  }

  fun save(config: ScriptConfig) {
    val path = getCacheFile(project)
    path.safeOutputStream().use {
      it.write(config.printJson().toByteArray())
    }
  }

}
<commit_msg>[space] Don't use readUTF8Line because it's implementation is not included in space-sdk<commit_after>package com.intellij.space.plugins.pipelines.services

import circlet.pipelines.config.api.ScriptConfig
import circlet.pipelines.config.api.parseProjectConfig
import circlet.pipelines.config.api.printJson
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.getProjectCachePath
import com.intellij.util.io.safeOutputStream
import libraries.klogging.logger
import java.io.IOException
import java.nio.file.FileSystemException
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes

private val log = logger<ScriptKtsPersistentState>()

class ScriptKtsPersistentState(val project: Project) {

  // not for load failure
  fun load(): ScriptConfig? {

    val path = getCacheFile(project)

    val fileAttributes = try {
      Files.readAttributes(path, BasicFileAttributes::class.java)
    }
    catch (ignored: FileSystemException) {
      return null
    }

    if (fileAttributes == null || !fileAttributes.isRegularFile) {
      return null
    }

    return try {
      Files.newBufferedReader(path, Charsets.UTF_8).use { reader ->
        reader.readLine().parseProjectConfig()
      }
    }
    catch (e: NoSuchFileException) {
      null
    }
    catch (e: IOException) {
      log.error(e)
      null
    }
  }

  private fun getCacheFile(project: Project): Path {
    return project.getProjectCachePath("space_automation").resolve(".space.kts.dat")
  }

  fun save(config: ScriptConfig) {
    val path = getCacheFile(project)
    path.safeOutputStream().use {
      it.write(config.printJson().toByteArray())
    }
  }

}

{'fraction_non_alphanumeric': 0.10103092783505155, 'fraction_numerical': 0.00117820324005891, 'mean_word_length': 3.8033946251768036, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4269759', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1001, 'n_words': 195}