summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/io/NullPrintStream.scala
blob: 52c7ddc74bb08404997fba2f3682f387c8a4f2b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.nsc
package io

import java.io.{ PrintStream, ByteArrayOutputStream }

/** A sink for when you want to discard all output.
 */
class NullPrintStream extends PrintStream(new ByteArrayOutputStream()) { }

object NullPrintStream extends NullPrintStream {
  def setOut() = Console setOut this
  def setErr() = Console setErr this
  def setOutAndErr() = { setOut() ; setErr() }
  def sinkingOutAndErr[T](body: => T): T =
    Console.withOut(this) {
      Console.withErr(this) {
        body
      }
    }

  def sinkingSystemOutAndErr[T](body: => T): T = {
    val savedOut = System.out
    val savedErr = System.err
    System setOut NullPrintStream
    System setErr NullPrintStream
    try body
    finally {
      System setOut savedOut
      System setErr savedErr
    }
  }
}