Packages

  • package root

    This is the documentation for the Scala standard library.

    This is the documentation for the Scala standard library.

    Package structure

    The scala package contains core types like Int, Float, Array or Option which are accessible in all Scala compilation units without explicit qualification or imports.

    Notable packages include:

    Other packages exist. See the complete list on the right.

    Additional parts of the standard library are shipped as separate libraries. These include:

    Automatic imports

    Identifiers in the scala package and the scala.Predef object are always in scope by default.

    Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example, List is an alias for scala.collection.immutable.List.

    Other aliases refer to classes provided by the underlying platform. For example, on the JVM, String is an alias for java.lang.String.

    Definition Classes
    root
  • package scala

    Core Scala types.

    Core Scala types. They are always available without an explicit import.

    Definition Classes
    root
  • package sys

    The package object scala.sys contains methods for reading and altering core aspects of the virtual machine as well as the world outside of it.

    The package object scala.sys contains methods for reading and altering core aspects of the virtual machine as well as the world outside of it.

    Definition Classes
    scala
  • package process

    This package handles the execution of external processes.

    This package handles the execution of external processes. The contents of this package can be divided in three groups, according to their responsibilities:

    • Indicating what to run and how to run it.
    • Handling a process input and output.
    • Running the process.

    For simple uses, the only group that matters is the first one. Running an external command can be as simple as "ls".!, or as complex as building a pipeline of commands such as this:

    import scala.sys.process._
    "ls" #| "grep .scala" #&& Seq("sh", "-c", "scalac *.scala") #|| "echo nothing found" lazyLines

    We describe below the general concepts and architecture of the package, and then take a closer look at each of the categories mentioned above.

    Concepts and Architecture

    The underlying basis for the whole package is Java's Process and ProcessBuilder classes. While there's no need to use these Java classes, they impose boundaries on what is possible. One cannot, for instance, retrieve a process id for whatever is executing.

    When executing an external process, one can provide a command's name, arguments to it, the directory in which it will be executed and what environment variables will be set. For each executing process, one can feed its standard input through a java.io.OutputStream, and read from its standard output and standard error through a pair of java.io.InputStream. One can wait until a process finishes execution and then retrieve its return value, or one can kill an executing process. Everything else must be built on those features.

    This package provides a DSL for running and chaining such processes, mimicking Unix shells ability to pipe output from one process to the input of another, or control the execution of further processes based on the return status of the previous one.

    In addition to this DSL, this package also provides a few ways of controlling input and output of these processes, going from simple and easy to use to complex and flexible.

    When processes are composed, a new ProcessBuilder is created which, when run, will execute the ProcessBuilder instances it is composed of according to the manner of the composition. If piping one process to another, they'll be executed simultaneously, and each will be passed a ProcessIO that will copy the output of one to the input of the other.

    What to Run and How

    The central component of the process execution DSL is the scala.sys.process.ProcessBuilder trait. It is ProcessBuilder that implements the process execution DSL, that creates the scala.sys.process.Process that will handle the execution, and return the results of such execution to the caller. We can see that DSL in the introductory example: #|, #&& and #!! are methods on ProcessBuilder used to create a new ProcessBuilder through composition.

    One creates a ProcessBuilder either through factories on the scala.sys.process.Process's companion object, or through implicit conversions available in this package object itself. Implicitly, each process is created either out of a String, with arguments separated by spaces -- no escaping of spaces is possible -- or out of a scala.collection.Seq, where the first element represents the command name, and the remaining elements are arguments to it. In this latter case, arguments may contain spaces.

    To further control what how the process will be run, such as specifying the directory in which it will be run, see the factories on scala.sys.process.Process's companion object.

    Once the desired ProcessBuilder is available, it can be executed in different ways, depending on how one desires to control its I/O, and what kind of result one wishes for:

    • Return status of the process (! methods)
    • Output of the process as a String (!! methods)
    • Continuous output of the process as a LazyList[String] (lazyLines methods)
    • The Process representing it (run methods)

    Some simple examples of these methods:

    import scala.sys.process._
    
    // This uses ! to get the exit code
    def fileExists(name: String) = Seq("test", "-f", name).! == 0
    
    // This uses !! to get the whole result as a string
    val dirContents = "ls".!!
    
    // This "fire-and-forgets" the method, which can be lazily read through
    // a LazyList[String]
    def sourceFilesAt(baseDir: String): LazyList[String] = {
      val cmd = Seq("find", baseDir, "-name", "*.scala", "-type", "f")
      cmd.lazyLines
    }

    We'll see more details about controlling I/O of the process in the next section.

    Handling Input and Output

    In the underlying Java model, once a Process has been started, one can get java.io.InputStream and java.io.OutputStream representing its output and input respectively. That is, what one writes to an OutputStream is turned into input to the process, and the output of a process can be read from an InputStream -- of which there are two, one representing normal output, and the other representing error output.

    This model creates a difficulty, which is that the code responsible for actually running the external processes is the one that has to take decisions about how to handle its I/O.

    This package presents an alternative model: the I/O of a running process is controlled by a scala.sys.process.ProcessIO object, which can be passed _to_ the code that runs the external process. A ProcessIO will have direct access to the java streams associated with the process I/O. It must, however, close these streams afterwards.

    Simpler abstractions are available, however. The components of this package that handle I/O are:

    Some examples of I/O handling:

    import scala.sys.process._
    
    // An overly complex way of computing size of a compressed file
    def gzFileSize(name: String) = {
      val cat = Seq("zcat", name)
      var count = 0
      def byteCounter(input: java.io.InputStream) = {
        while(input.read() != -1) count += 1
        input.close()
      }
      val p = cat run new ProcessIO(_.close(), byteCounter, _.close())
      p.exitValue()
      count
    }
    
    // This "fire-and-forgets" the method, which can be lazily read through
    // a LazyList[String], and accumulates all errors on a StringBuffer
    def sourceFilesAt(baseDir: String): (LazyList[String], StringBuffer) = {
      val buffer = new StringBuffer()
      val cmd = Seq("find", baseDir, "-name", "*.scala", "-type", "f")
      val lazyLines = cmd lazyLines_! ProcessLogger(buffer append _)
      (lazyLines, buffer)
    }

    Instances of the java classes java.io.File and java.net.URL can both be used directly as input to other processes, and java.io.File can be used as output as well. One can even pipe one to the other directly without any intervening process, though that's not a design goal or recommended usage. For example, the following code will copy a web page to a file:

    import java.io.File
    import java.net.URL
    import scala.sys.process._
    new URL("https://www.scala-lang.org/") #> new File("scala-lang.html") !

    More information about the other ways of controlling I/O can be found in the Scaladoc for the associated objects, traits and classes.

    Running the Process

    Paradoxically, this is the simplest component of all, and the one least likely to be interacted with. It consists solely of scala.sys.process.Process, and it provides only two methods:

    • exitValue(): blocks until the process exit, and then returns the exit value. This is what happens when one uses the ! method of ProcessBuilder.
    • destroy(): this will kill the external process and close the streams associated with it.
    Definition Classes
    sys
  • BasicIO
  • FileProcessLogger
  • Process
  • ProcessBuilder
  • ProcessCreation
  • ProcessIO
  • ProcessImplicits
  • ProcessLogger

object BasicIO

This object contains factories for scala.sys.process.ProcessIO, which can be used to control the I/O of a scala.sys.process.Process when a scala.sys.process.ProcessBuilder is started with the run command.

It also contains some helper methods that can be used to in the creation of ProcessIO.

It is used by other classes in the package in the implementation of various features, but can also be used by client code.

Source
BasicIO.scala
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. BasicIO
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Value Members

  1. final def !=(arg0: Any): Boolean

    Test two objects for inequality.

    Test two objects for inequality.

    returns

    true if !(this == that), false otherwise.

    Definition Classes
    AnyRef → Any
  2. final def ##: Int

    Equivalent to x.hashCode except for boxed numeric types and null.

    Equivalent to x.hashCode except for boxed numeric types and null. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. For null returns a hashcode where null.hashCode throws a NullPointerException.

    returns

    a hash value consistent with ==

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).

    The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that).

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    Definition Classes
    AnyRef → Any
  4. final val BufferSize: Int(8192)

    Size of the buffer used in all the functions that copy data

  5. final val Newline: String

    Used to separate lines in the processFully function that takes Appendable.

  6. def apply(withIn: Boolean, log: ProcessLogger): ProcessIO

    Creates a ProcessIO from a ProcessLogger .

    Creates a ProcessIO from a ProcessLogger . It can attach the process input to stdin.

    withIn

    True if the process input should be attached to stdin.

    log

    A ProcessLogger to receive all output, normal and error.

    returns

    A ProcessIO with the characteristics above.

  7. def apply(withIn: Boolean, buffer: Appendable, log: Option[ProcessLogger]): ProcessIO

    Creates a ProcessIO that appends its output to an Appendable.

    Creates a ProcessIO that appends its output to an Appendable. It can attach the process input to stdin, and it will either send the error stream to stderr, or to a ProcessLogger.

    For example, the ProcessIO created by the function below will store the normal output on the buffer provided, and print all error on stderr. The input will be read from stdin.

    import scala.sys.process.{BasicIO, ProcessLogger}
    val printer = ProcessLogger(println(_))
    def appendToBuffer(b: StringBuffer) = BasicIO(true, b, Some(printer))
    withIn

    True if the process input should be attached to stdin.

    buffer

    An Appendable which will receive the process normal output.

    log

    An optional ProcessLogger to which the output should be sent. If None, output will be sent to stderr.

    returns

    A ProcessIO with the characteristics above.

  8. def apply(withIn: Boolean, output: (String) => Unit, log: Option[ProcessLogger]): ProcessIO

    Creates a ProcessIO from a function String => Unit.

    Creates a ProcessIO from a function String => Unit. It can attach the process input to stdin, and it will either send the error stream to stderr, or to a ProcessLogger.

    For example, the ProcessIO created below will print all normal output while ignoring all error output. No input will be provided.

    import scala.sys.process.BasicIO
    val errToDevNull = BasicIO(false, println(_), None)
    withIn

    True if the process input should be attached to stdin.

    output

    A function that will be called with the process output.

    log

    An optional ProcessLogger to which the output should be sent. If None, output will be sent to stderr.

    returns

    A ProcessIO with the characteristics above.

  9. final def asInstanceOf[T0]: T0

    Cast the receiver object to be of type T0.

    Cast the receiver object to be of type T0.

    Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String] will throw a ClassCastException at runtime, while the expression List(1).asInstanceOf[List[String]] will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.

    returns

    the receiver object.

    Definition Classes
    Any
    Exceptions thrown

    ClassCastException if the receiver object is not an instance of the erasure of type T0.

  10. def clone(): AnyRef

    Create a copy of the receiver object.

    Create a copy of the receiver object.

    The default implementation of the clone method is platform dependent.

    returns

    a copy of the receiver object.

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
    Note

    not specified by SLS as a member of AnyRef

  11. def close(c: Closeable): Unit

    Closes a Closeable without throwing an exception

  12. def connectToIn(o: OutputStream): Unit

    Copy contents of stdin to the OutputStream.

  13. final def eq(arg0: AnyRef): Boolean

    Tests whether the argument (that) is a reference to the receiver object (this).

    Tests whether the argument (that) is a reference to the receiver object (this).

    The eq method implements an equivalence relation on non-null instances of AnyRef, and has three additional properties:

    • It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false.
    • For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false.
    • null.eq(null) returns true.

    When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).

    returns

    true if the argument is a reference to the receiver object; false otherwise.

    Definition Classes
    AnyRef
  14. def equals(arg0: AnyRef): Boolean

    The equality method for reference types.

    The equality method for reference types. Default implementation delegates to eq.

    See also equals in scala.Any.

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    Definition Classes
    AnyRef → Any
  15. def finalize(): Unit

    Called by the garbage collector on the receiver object when there are no more references to the object.

    Called by the garbage collector on the receiver object when there are no more references to the object.

    The details of when and if the finalize method is invoked, as well as the interaction between finalize and non-local returns and exceptions, are all platform dependent.

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
    Note

    not specified by SLS as a member of AnyRef

  16. final def getClass(): Class[_ <: AnyRef]

    Returns the runtime class representation of the object.

    Returns the runtime class representation of the object.

    returns

    a class object corresponding to the runtime type of the receiver.

    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  17. def getErr(log: Option[ProcessLogger]): (InputStream) => Unit

    Returns a function InputStream => Unit given an optional ProcessLogger.

    Returns a function InputStream => Unit given an optional ProcessLogger. If no logger is passed, the function will send the output to stderr. This function can be used to create a scala.sys.process.ProcessIO.

    log

    An optional ProcessLogger to which the contents of the InputStream will be sent.

    returns

    A function InputStream => Unit (used by scala.sys.process.ProcessIO) which will send the data to either the provided ProcessLogger or, if None, to stderr.

  18. def hashCode(): Int

    The hashCode method for reference types.

    The hashCode method for reference types. See hashCode in scala.Any.

    returns

    the hash code value for this object.

    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  19. def input(connect: Boolean): (OutputStream) => Unit

    Returns a function OutputStream => Unit that either reads the content from stdin or does nothing but close the stream.

    Returns a function OutputStream => Unit that either reads the content from stdin or does nothing but close the stream. This function can be used by scala.sys.process.ProcessIO.

  20. final def isInstanceOf[T0]: Boolean

    Test whether the dynamic type of the receiver object is T0.

    Test whether the dynamic type of the receiver object is T0.

    Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String] will return false, while the expression List(1).isInstanceOf[List[String]] will return true. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.

    returns

    true if the receiver object is an instance of erasure of type T0; false otherwise.

    Definition Classes
    Any
  21. final def ne(arg0: AnyRef): Boolean

    Equivalent to !(this eq that).

    Equivalent to !(this eq that).

    returns

    true if the argument is not a reference to the receiver object; false otherwise.

    Definition Classes
    AnyRef
  22. final def notify(): Unit

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Definition Classes
    AnyRef
    Annotations
    @native()
    Note

    not specified by SLS as a member of AnyRef

  23. final def notifyAll(): Unit

    Wakes up all threads that are waiting on the receiver object's monitor.

    Wakes up all threads that are waiting on the receiver object's monitor.

    Definition Classes
    AnyRef
    Annotations
    @native()
    Note

    not specified by SLS as a member of AnyRef

  24. def processFully(processLine: (String) => Unit): (InputStream) => Unit

    Returns a function InputStream => Unit that will call the passed function with all data read.

    Returns a function InputStream => Unit that will call the passed function with all data read. This function can be used to create a scala.sys.process.ProcessIO. The processLine function will be called with each line read, and Newline will be appended after each line.

    processLine

    A function that will be called with all data read from the stream.

    returns

    A function InputStream => Unit (used by scala.sys.process.ProcessIO which will call processLine with all data read from the stream.

  25. def processFully(buffer: Appendable): (InputStream) => Unit

    Returns a function InputStream => Unit that appends all data read to the provided Appendable.

    Returns a function InputStream => Unit that appends all data read to the provided Appendable. This function can be used to create a scala.sys.process.ProcessIO. The buffer will be appended line by line.

    buffer

    An Appendable such as StringBuilder or StringBuffer.

    returns

    A function InputStream => Unit (used by scala.sys.process.ProcessIO which will append all data read from the stream to the buffer.

  26. def processLinesFully(processLine: (String) => Unit)(readLine: () => String): Unit

    Calls processLine with the result of readLine until the latter returns null or the current thread is interrupted.

  27. def standard(in: (OutputStream) => Unit): ProcessIO

    Returns a ProcessIO connected to stdout, stderr and the provided in

  28. def standard(connectInput: Boolean): ProcessIO

    Returns a ProcessIO connected to stdout and stderr, and, optionally, stdin.

  29. final def synchronized[T0](arg0: => T0): T0

    Executes the code in body with an exclusive lock on this.

    Executes the code in body with an exclusive lock on this.

    returns

    the result of body

    Definition Classes
    AnyRef
  30. def toStdErr: (InputStream) => Unit

    Send all the input from the stream to stderr, and closes the input stream afterwards.

  31. def toStdOut: (InputStream) => Unit

    Send all the input from the stream to stdout, and closes the input stream afterwards.

  32. def toString(): String

    Creates a String representation of this object.

    Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.

    returns

    a String representation of the object.

    Definition Classes
    AnyRef → Any
  33. def transferFully(in: InputStream, out: OutputStream): Unit

    Copy all input from the input stream to the output stream.

    Copy all input from the input stream to the output stream. Closes the input stream once it's all read.

  34. final def wait(): Unit

    See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--.

    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
    Note

    not specified by SLS as a member of AnyRef

  35. final def wait(arg0: Long, arg1: Int): Unit

    See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-

    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
    Note

    not specified by SLS as a member of AnyRef

  36. final def wait(arg0: Long): Unit

    See https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-.

    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
    Note

    not specified by SLS as a member of AnyRef

Inherited from AnyRef

Inherited from Any

Ungrouped