summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/TeeInputStream.java
blob: cf4af87da45ff2adc28f49dbcda71d556c0feba2 (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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

/**
 * Forwards input from a source InputStream while making a copy of it into an outputstream.
 * Note that it also does read-ahead and copies up to 64K of data more than was used.
 *
 * @author arnej
 */
class TeeInputStream extends InputStream {
    final InputStream src;
    final OutputStream dst;

    static final int CAPACITY = 65536;

    byte[] buf = new byte[CAPACITY];
    int readPos = 0;
    int writePos = 0;

    private int inBuf() { return writePos - readPos; }

    private void fillBuf() throws IOException {
        if (readPos == writePos) {
            readPos = 0;
            writePos = 0;
        }
        if (readPos * 3 > CAPACITY) {
            int had = inBuf();
            System.arraycopy(buf, readPos, buf, 0, had);
            readPos = 0;
            writePos = had;
        }
        int wantToRead = CAPACITY - writePos;
        if (inBuf() > 0) {
            // if we have data already, do not block, read only what is available
            wantToRead = Math.min(wantToRead, src.available());
        }
        if (wantToRead > 0) {
            int got = src.read(buf, writePos, wantToRead);
            if (got > 0) {
                dst.write(buf, writePos, got);
                writePos += got;
            }
        }
    }

    /** Construct a Tee */
    public TeeInputStream(InputStream from, OutputStream to) {
        super();
        this.src = from;
        this.dst = to;
    }

    @Override
    public int available() throws IOException {
        return inBuf() + src.available();
    }

    @Override
    public void close() throws IOException {
        src.close();
        dst.close();
    }

    @Override
    public int read() throws IOException {
        fillBuf();
        if (inBuf() > 0) {
            int r = buf[readPos++];
            return r & 0xff;
        }
        return -1;
    }

    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (len <= 0) {
            return 0;
        }
        fillBuf();
        int had = inBuf();
        if (had > 0) {
            len = Math.min(len, had);
            System.arraycopy(buf, readPos, b, off, len);
            readPos += len;
            return len;
        }
        return -1;
    }

}