-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBasicIO.scala
75 lines (69 loc) · 2.43 KB
/
BasicIO.scala
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* sbt -- Simple Build Tool
* Copyright 2009 Mark Harrah, Vesa Vilhonen
*/
package xsbt
import java.io.{BufferedReader, Closeable, FilterInputStream, FilterOutputStream, InputStream, InputStreamReader, IOException, OutputStream}
private object BasicIO
{
def ignoreOut = (i: OutputStream) => ()
val BufferSize = 8192
def close(c: java.io.Closeable) = try { c.close() } catch { case _: java.io.IOException => () }
def processFully(processLine: String => Unit)(i: InputStream)
{
val reader = new BufferedReader(new InputStreamReader(i))
processLinesFully(processLine)(reader.readLine)
}
def processLinesFully(processLine: String => Unit)(readLine: () => String)
{
def readFully()
{
val line = readLine()
if(line != null)
{
processLine(line)
readFully()
}
}
readFully()
}
def connectToIn(o: OutputStream) { transferFully(System.in, o) }
def input(connect: Boolean): OutputStream => Unit = if(connect) connectToIn else ignoreOut
def standard(connectInput: Boolean): ProcessIO = standard(input(connectInput))
def standard(in: OutputStream => Unit): ProcessIO = new ProcessIO(in, transferFully(_, System.out), transferFully(_, System.err))
def transferFully(in: InputStream, out: OutputStream): Unit =
try { transferFullyImpl(in, out) }
catch { case _: InterruptedException => () }
private[this] def transferFullyImpl(in: InputStream, out: OutputStream)
{
val continueCount = 1//if(in.isInstanceOf[PipedInputStream]) 1 else 0
val buffer = new Array[Byte](BufferSize)
def read
{
val byteCount = in.read(buffer)
if(byteCount >= continueCount)
{
out.write(buffer, 0, byteCount)
out.flush()
read
}
}
read
}
}
object Uncloseable
{
def apply(in: InputStream): InputStream = new FilterInputStream(in) { override def close() {} }
def apply(out: OutputStream): OutputStream = new FilterOutputStream(out) { override def close() {} }
def protect(in: InputStream): InputStream = if(in eq System.in) Uncloseable(in) else in
def protect(out: OutputStream): OutputStream = if( (out eq System.out) || (out eq System.err)) Uncloseable(out) else out
}
protected[xsbt] object runInterruptible
{
/** Evaluates 'action' and returns it wrapped in Some. If this thread is interrupted before 'action' completes,
* 'destroyImpl' is called and None is returned.*/
def apply[T](action: => T)(destroyImpl: => Unit): Option[T] =
{
try { Some(action) }
catch { case _: InterruptedException => destroyImpl; None }
}
}