<commit_before>package org.jetbrains.jps.incremental.scala
package remote

import local.LocalServer
import com.intellij.util.Base64Converter
import com.martiansoftware.nailgun.{ThreadLocalPrintStream, NGContext}
import java.io.{OutputStream, PrintStream, File}
import java.net.URLClassLoader
import org.jetbrains.jps.incremental.scala.data.CompilerJars
import java.nio.ByteBuffer
import java.lang.reflect.InvocationTargetException
import org.jetbrains.jps.incremental.messages.BuildMessage.Kind

/**
 * @author <NAME>
 * @author <NAME>         
 */
object Main {
  private val Server = new LocalServer()

  def nailMain(context: NGContext) {
    make(context.getArgs.toSeq, context.out, false)
  }
  
  def main(args: Array[String]) {
    make(args, System.out, true)
  }
  
  private def make(arguments: Seq[String], out: PrintStream, standalone: Boolean) {
    var hasErrors = false

    val client = {
      val eventHandler = (event: Event) => {
        val encode = Base64Converter.encode(event.toBytes)
        out.write((if (standalone && !encode.endsWith("=")) encode + "=" else encode).getBytes)
      }
      new EventGeneratingClient(eventHandler, out.checkError) {
        override def error(text: String, source: Option[File], line: Option[Long], column: Option[Long]) {
          hasErrors = true
          super.error(text, source, line, column)
        }

        override def message(kind: Kind, text: String, source: Option[File], line: Option[Long], column: Option[Long]) {
          if (kind == Kind.ERROR) hasErrors = true
          super.message(kind, text, source, line, column)
        }
      }
    }

    val oldOut = System.out
    // Suppress any stdout data, interpret such data as error
    System.setOut(System.err)
    
    try {
      val args = {
        val strings = arguments.map {
          arg => 
            val s = new String(Base64Converter.decode(arg.getBytes), "UTF-8")
            if (s == "#STUB#") "" else s
        }
        Arguments.from(strings)
      }
      
      Server.compile(args.sbtData, args.compilerData, args.compilationData, client)

      if (!hasErrors) new WorksheetInProcessRunner(out, standalone).loadAndRun(args, client)
    } catch {
      case e: Throwable => 
        client.trace(e)
    } finally {
      System.setOut(oldOut)
    }
  }
  
  
  private class WorksheetInProcessRunner(out: PrintStream, standalone: Boolean) {
    private val TRACE_PREFIX = 21
    private val WORKSHEET = "#worksheet#"
    
    private val myOut = new OutputStream {
      private var capacity = 1200
      private var buffer = ByteBuffer.allocate(capacity)

      override def write(b: Int) {
        if (b == '\r') return

        if (buffer.position() < capacity) buffer.put(b.toByte) else {
          val copy = buffer.array().clone()
          capacity *= 2
          buffer = ByteBuffer.allocate(capacity)
          buffer.put(copy)
        }

        if (b == '\n') flush()
      }

      override def close() {
        flush()
      }

      override def flush() {
        if (buffer.position() == 0) return
        val event = WorksheetOutputEvent(new String(buffer.array(), 0, buffer.position()))
        buffer.clear()
        val encode = Base64Converter.encode(event.toBytes)
        out.write(if (standalone && !encode.endsWith("=")) (encode + "=").getBytes else encode.getBytes)
      }
    }
    
    def loadAndRun(arguments: Arguments, client: EventGeneratingClient) {
      arguments.worksheetFiles.headOption.map {
        case className =><commit_msg>{Worksheet output error with long strings} #SCL-7179 fixed<commit_after>package org.jetbrains.jps.incremental.scala
package remote

import local.LocalServer
import com.intellij.util.Base64Converter
import com.martiansoftware.nailgun.{ThreadLocalPrintStream, NGContext}
import java.io.{OutputStream, PrintStream, File}
import java.net.URLClassLoader
import org.jetbrains.jps.incremental.scala.data.CompilerJars
import java.nio.ByteBuffer
import java.lang.reflect.InvocationTargetException
import org.jetbrains.jps.incremental.messages.BuildMessage.Kind

/**
 * @author <NAME>
 * @author <NAME>         
 */
object Main {
  private val Server = new LocalServer()

  def nailMain(context: NGContext) {
    make(context.getArgs.toSeq, context.out, false)
  }
  
  def main(args: Array[String]) {
    make(args, System.out, true)
  }
  
  private def make(arguments: Seq[String], out: PrintStream, standalone: Boolean) {
    var hasErrors = false

    val client = {
      val eventHandler = (event: Event) => {
        val encode = Base64Converter.encode(event.toBytes)
        out.write((if (standalone && !encode.endsWith("=")) encode + "=" else encode).getBytes)
      }
      new EventGeneratingClient(eventHandler, out.checkError) {
        override def error(text: String, source: Option[File], line: Option[Long], column: Option[Long]) {
          hasErrors = true
          super.error(text, source, line, column)
        }

        override def message(kind: Kind, text: String, source: Option[File], line: Option[Long], column: Option[Long]) {
          if (kind == Kind.ERROR) hasErrors = true
          super.message(kind, text, source, line, column)
        }
      }
    }

    val oldOut = System.out
    // Suppress any stdout data, interpret such data as error
    System.setOut(System.err)
    
    try {
      val args = {
        val strings = arguments.map {
          arg => 
            val s = new String(Base64Converter.decode(arg.getBytes), "UTF-8")
            if (s == "#STUB#") "" else s
        }
        Arguments.from(strings)
      }
      
      Server.compile(args.sbtData, args.compilerData, args.compilationData, client)

      if (!hasErrors) new WorksheetInProcessRunner(out, standalone).loadAndRun(args, client)
    } catch {
      case e: Throwable => 
        client.trace(e)
    } finally {
      System.setOut(oldOut)
    }
  }
  
  
  private class WorksheetInProcessRunner(out: PrintStream, standalone: Boolean) {
    private val TRACE_PREFIX = 21
    private val WORKSHEET = "#worksheet#"
    
    private val myOut = new OutputStream {
      private var capacity = 1200
      private var buffer = ByteBuffer.allocate(capacity)

      override def write(b: Int) {
        if (b == '\r') return

        if (buffer.position() < capacity) buffer.put(b.toByte) else {
          val copy = buffer.array().clone()
          capacity *= 2
          buffer = ByteBuffer.allocate(capacity)
          buffer put copy
          buffer put b.toByte
        }

        if (b == '\n') flush()
      }

      override def close() {
        flush()
      }

      override def flush() {
        if (buffer.position() == 0) return
        val event = WorksheetOutputEvent(new String(buffer.array(), 0, buffer.position()))
        buffer.clear()
        val encode = Base64Converter.encode(event.toBytes)
        out.write(if (standalone && !encode.endsWith("=")) (encode + "=").getBytes else encode.getBytes)
      }
    }
    
    def loadAndRun(arguments: Arguments, client: EventGeneratingClient) {
      arguments.worksheetFiles.headOption.map {
        case className =>
{'fraction_non_alphanumeric': 0.1155680224403927, 'fraction_numerical': 0.005610098176718092, 'mean_word_length': 2.671987641606591, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '5012847', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2065, 'n_words': 514}