1+ package com.tschuchort.compiletest
2+
3+ import java.io.IOException
4+ import java.io.OutputStream
5+
6+ /* * An output stream that does nothing, like /dev/null */
7+ internal object NullStream : OutputStream() {
8+ override fun write (b : Int ) {
9+ // NoOp
10+ }
11+
12+ override fun close () {
13+ // NoOp
14+ }
15+
16+ override fun flush () {
17+ // NoOp
18+ }
19+
20+ override fun write (b : ByteArray , off : Int , len : Int ) {
21+ // NoOp
22+ }
23+
24+ override fun write (b : ByteArray ) {
25+ // NoOp
26+ }
27+ }
28+
29+ /* * A combined stream that writes to all the output streams in [streams]. */
30+ @Suppress(" MemberVisibilityCanBePrivate" )
31+ internal class TeeOutputStream (val streams : Collection <OutputStream >) : OutputStream() {
32+
33+ constructor (vararg streams: OutputStream ) : this (streams.toList())
34+
35+ @Synchronized
36+ @Throws(IOException ::class )
37+ override fun write (b : Int ) {
38+ for (stream in streams)
39+ stream.write(b)
40+ }
41+
42+ @Synchronized
43+ @Throws(IOException ::class )
44+ override fun write (b : ByteArray ) {
45+ for (stream in streams)
46+ stream.write(b)
47+ }
48+
49+ @Synchronized
50+ @Throws(IOException ::class )
51+ override fun write (b : ByteArray , off : Int , len : Int ) {
52+ for (stream in streams)
53+ stream.write(b, off, len)
54+ }
55+
56+ @Throws(IOException ::class )
57+ override fun flush () {
58+ for (stream in streams)
59+ stream.flush()
60+ }
61+
62+ @Throws(IOException ::class )
63+ override fun close () {
64+ closeImpl(streams)
65+ }
66+
67+ @Throws(IOException ::class )
68+ private fun closeImpl (streamsToClose : Collection <OutputStream >) {
69+ try {
70+ streamsToClose.firstOrNull()?.close()
71+ }
72+ finally {
73+ if (streamsToClose.size > 1 )
74+ closeImpl(streamsToClose.drop(1 ))
75+ }
76+ }
77+ }
0 commit comments