<commit_before>package scribe.writer

import java.io
import java.io.PrintWriter
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.charset.Charset
import java.nio.file._

import scala.annotation.tailrec

case class FileIOWriter(directory: Path,
                      fileNameGenerator: () => String,
                      append: Boolean = true,
                      autoFlush: Boolean = true,
                      charset: Charset = Charset.defaultCharset()) extends FileWriter {
  private var writer: Option[PrintWriter] = None
  private var currentFileName: Option[String] = None

  override def write(output: String): Unit = {
    validateFileName()
    val writer = validateWriter()
    writer.write(output)
  }

  @tailrec
  private def writeBuffer(buffer: ByteBuffer, channel: FileChannel): Unit = if (buffer.hasRemaining) {
    channel.write(buffer)
    writeBuffer(buffer, channel)
  }

  protected def validateFileName(): Unit = {
    val fileName: String = fileNameGenerator()
    if (!currentFileName.contains(fileName)) {    // Changed
      writer.foreach { w =>
        w.flush()
        w.close()
      }
      writer = None
    }
    currentFileName = Some(fileName)
  }

  protected def validateWriter(): PrintWriter = writer match {
    case Some(w) => w
    case None => {
      if (!Files.exists(directory)) {       // Create the directories if it doesn't exist
        Files.createDirectories(directory)
      }
      val path = directory.resolve(currentFileName.getOrElse(throw new RuntimeException("File name cannot be empty!")))
      val file = path.toFile
      val w = new PrintWriter(new io.FileWriter(file, append))
      writer = Some(w)
      w
    }
  }

  override def dispose(): Unit = {
    super.dispose()

    writer.foreach(_.close())
  }
}

object FileIOWriter {
  def single(prefix: String = "app",
             suffix: String = ".log",
             directory: Path = Paths.get("logs"),
             append: Boolean = true,
             autoFlush: Boolean = true,
             charset: Charset = Charset.defaultCharset()): FileNIOWriter = {
    new FileNIOWriter(directory, FileWriter.generator.single(prefix, suffix), append, autoFlush, charset)
  }

  def daily(prefix: String = "app",
            suffix: String = ".log",
            directory: Path = Paths.get("logs"),
            append: Boolean = true,
            autoFlush: Boolean = true,
            charset: Charset = Charset.defaultCharset()): FileNIOWriter = {
    new FileNIOWriter(directory, FileWriter.generator.daily(prefix, suffix), append, autoFlush, charset)
  }
}<commit_msg>use FileIOWriter in FileIOWriter factory<commit_after>package scribe.writer

import java.io
import java.io.PrintWriter
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.charset.Charset
import java.nio.file._

import scala.annotation.tailrec

case class FileIOWriter(directory: Path,
                      fileNameGenerator: () => String,
                      append: Boolean = true,
                      autoFlush: Boolean = true,
                      charset: Charset = Charset.defaultCharset()) extends FileWriter {
  private var writer: Option[PrintWriter] = None
  private var currentFileName: Option[String] = None

  override def write(output: String): Unit = {
    validateFileName()
    val writer = validateWriter()
    writer.write(output)
  }

  @tailrec
  private def writeBuffer(buffer: ByteBuffer, channel: FileChannel): Unit = if (buffer.hasRemaining) {
    channel.write(buffer)
    writeBuffer(buffer, channel)
  }

  protected def validateFileName(): Unit = {
    val fileName: String = fileNameGenerator()
    if (!currentFileName.contains(fileName)) {    // Changed
      writer.foreach { w =>
        w.flush()
        w.close()
      }
      writer = None
    }
    currentFileName = Some(fileName)
  }

  protected def validateWriter(): PrintWriter = writer match {
    case Some(w) => w
    case None => {
      if (!Files.exists(directory)) {       // Create the directories if it doesn't exist
        Files.createDirectories(directory)
      }
      val path = directory.resolve(currentFileName.getOrElse(throw new RuntimeException("File name cannot be empty!")))
      val file = path.toFile
      val w = new PrintWriter(new io.FileWriter(file, append))
      writer = Some(w)
      w
    }
  }

  override def dispose(): Unit = {
    super.dispose()

    writer.foreach(_.close())
  }
}

object FileIOWriter {
  def single(prefix: String = "app",
             suffix: String = ".log",
             directory: Path = Paths.get("logs"),
             append: Boolean = true,
             autoFlush: Boolean = true,
             charset: Charset = Charset.defaultCharset()): FileIOWriter = {
    new FileIOWriter(directory, FileWriter.generator.single(prefix, suffix), append, autoFlush, charset)
  }

  def daily(prefix: String = "app",
            suffix: String = ".log",
            directory: Path = Paths.get("logs"),
            append: Boolean = true,
            autoFlush: Boolean = true,
            charset: Charset = Charset.defaultCharset()): FileIOWriter = {
    new FileIOWriter(directory, FileWriter.generator.daily(prefix, suffix), append, autoFlush, charset)
  }
}
{'fraction_non_alphanumeric': 0.1075841094848888, 'fraction_numerical': 0.0, 'mean_word_length': 2.661795407098121, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7147205', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1453, 'n_words': 384}