summaryrefslogtreecommitdiff
path: root/support/arara/source/src/main/kotlin/org/islandoftex/arara/utils/TeeOutputStream.kt
blob: dee15176f24624b69c39b31a9f2a887663e466dc (plain)
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
// SPDX-License-Identifier: BSD-3-Clause
package org.islandoftex.arara.utils

import java.io.IOException
import java.io.OutputStream

/**
 * Implements a stream splitter.
 *
 * @author Island of TeX
 * @version 5.0
 * @since 4.0
 */
class TeeOutputStream(
    /**
     * The array of output streams holds every output stream that will be
     * written to.
     */
    vararg outputStreams: OutputStream
) : OutputStream() {
    /**
     * An array of streams in which an object of this class will split data.
     */
    private val streams: List<OutputStream> = outputStreams.toList()

    /**
     * Writes the provided integer to each stream.
     *
     * @param b The provided integer
     * @throws IOException An IO exception.
     */
    @Throws(IOException::class)
    override fun write(b: Int) = streams.forEach { it.write(b) }

    /**
     * Writes the provided byte array to each stream, with the provided offset
     * and length.
     *
     * @param b The byte array.
     * @param offset The offset.
     * @param length The length.
     * @throws IOException An IO exception.
     */
    @Throws(IOException::class)
    override fun write(b: ByteArray, offset: Int, length: Int) =
            streams.forEach { it.write(b, offset, length) }

    /**
     * Flushes every stream.
     *
     * @throws IOException An IO exception.
     */
    @Throws(IOException::class)
    override fun flush() = streams.forEach { it.flush() }

    /**
     * Closes every stream silently.
     */
    override fun close() = streams.forEach {
        try {
            it.close()
        } catch (ignored: IOException) {
            // do nothing on purpose
        }
    }
}