All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ossuminc.riddl.utils.StringBuildingPrintStream.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2019 Ossum, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package com.ossuminc.riddl.utils

import java.io.OutputStream
import java.io.PrintStream
import scala.collection.mutable
import scala.scalajs.js.annotation.JSExportTopLevel

/** A PrintStream that captures any output into a String */
final class StringBuildingPrintStream private (out: OutputStream, stringBuilder: mutable.StringBuilder)
    extends PrintStream(out) {

  /** Returns a String of all output written to this PrintStream */
  def mkString(): String = stringBuilder.mkString
}

@JSExportTopLevel("StringBuildingPrintStream")
object StringBuildingPrintStream {

  /** Returns a new StringBuildingPrintStream */
  def apply(): StringBuildingPrintStream = {
    val stringBuilder = new mutable.StringBuilder
    val out = new OutputStream {
      override def write(b: Int): Unit = stringBuilder.append(b.toChar)
    }
    new StringBuildingPrintStream(out, stringBuilder)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy