aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/federation/http/TimedStream.java
blob: 77a42ee0a343880f1d44b4b478a53dcaa0df3f37 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.federation.http;

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

/**
 * A stream which throws a TimeoutException if query timeout has been reached.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 * @deprecated
 */
// TODO: Remove on Vespa 7
@Deprecated // OK
public class TimedStream extends InputStream {

    /**
     * A time barrier value, the point in time from which on read operations will cause an exception.
     */
    private final long limit;

    /**
     * A wrapped InputStream instance.
     */
    private final InputStream content;

    /**
     * Wrap an InputStream to make read operations potentially fire off
     * TimeoutException.
     *
     * <p>Typical use would be<br>
     * <code>new TimedStream(httpEntity.getContent(), query.getStartTime(), query.getTimeout())</code>
     *
     * @param content
     *                the InputStream to wrap
     * @param startTime
     *                start time of query
     * @param timeout
     *                how long the query is allowed to run
     */
    public TimedStream(InputStream content, long startTime, long timeout) {
        if (content == null) {
            throw new IllegalArgumentException("Cannot instantiate TimedStream with null InputStream");
        }
        this.content = content;
        // The reasion for doing it in here instead of outside the constructor
        // is this makes the usage of the class more intuitive IMHO
        this.limit = startTime + timeout;
    }

    private void checkTime(String message) {
        if (System.currentTimeMillis() >= limit) {
            throw new TimeoutException(message);
        }
    }

    // START FORWARDING METHODS:
    // All methods below are forwarding methods to the contained stream, where
    // some do a timeout check.
    @Override
    public int read() throws IOException {
        int data = content.read();
        checkTime("Timed out during read().");
        return data;
    }

    @Override
    public int available() throws IOException {
        return content.available();
    }

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

    @Override
    public synchronized void mark(int readlimit) {
        content.mark(readlimit);
    }

    @Override
    public boolean markSupported() {
        return content.markSupported();
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int length = content.read(b, off, len);
        checkTime("Timed out during read(byte[], int, int)");
        return length;
    }

    @Override
    public int read(byte[] b) throws IOException {
        int length = content.read(b);
        checkTime("Timed out during read(byte[])");
        return length;
    }

    @Override
    public synchronized void reset() throws IOException {
        content.reset();
    }

    @Override
    public long skip(long n) throws IOException {
        long skipped = content.skip(n);
        checkTime("Timed out during skip(long)");
        return skipped;
    }
    // END FORWARDING METHODS

}