Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 25c44e490e0c293ac408dbae38c6c0ce4a5e988a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************************
 * Copyright (c) 2009, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.launch.setup;

import java.io.*;

class TimeOutInputStream extends FilterInputStream {

    private byte buf[];
    private int buf_inp = 0;
    private int buf_out = 0;
    private boolean eof = false;
    private IOException err;
    private InputStream inp;
    private long time = 0;
    private Thread thread;

    private class Prefetch extends Thread {

        public void run() {
            try {
                for (;;) {
                    int rd = inp.read();
                    if (rd < 0) break;
                    synchronized (TimeOutInputStream.this) {
                        int new_inp = (buf_inp + 1) % buf.length;
                        while (new_inp == buf_out) TimeOutInputStream.this.wait();
                        buf[buf_inp] = (byte)rd;
                        buf_inp = new_inp;
                        TimeOutInputStream.this.notify();
                    }
                }
            }
            catch (InterruptedException x) {
                err = new InterruptedIOException();
            }
            catch (InterruptedIOException x) {
                err = x;
            }
            catch (IOException x) {
                err = x;
            }
            finally {
                synchronized (TimeOutInputStream.this) {
                    eof = true;
                    TimeOutInputStream.this.notify();
                }
            }
        }
    }

    TimeOutInputStream(InputStream in, long time) {
        this(in, 0x1000, time);
    }

    TimeOutInputStream(InputStream in, int size, long time) {
        super(in);
        inp = in;
        buf = new byte[size];
        this.time = time;
        thread = new Prefetch();
        thread.start();
    }

    public void setTimeOut(long time) {
        this.time = time;
    }

    public synchronized int read() throws IOException {
        try {
            int cnt = 0;
            while (buf_inp == buf_out) {
                if (err != null) throw new IOException(err.getMessage());
                if (eof) return -1;
                if (time != 0) {
                    if (cnt > 0) throw new IOException("Time Out");
                    wait(time);
                    cnt++;
                }
                else {
                    wait();
                }
            }
            int res = buf[buf_out] & 0xff;
            buf_out = (buf_out + 1) % buf.length;
            notify();
            return res;
        }
        catch (InterruptedException x) {
            throw new InterruptedIOException();
        }
    }

    public synchronized int read(byte b[], int off, int len) throws IOException {
        boolean nfy = false;
        try {
            int res = 0;
            while (res < len) {
                int cnt = 0;
                while (buf_inp == buf_out) {
                    if (res > 0) return res;
                    if (err != null) throw new IOException(err.getMessage());
                    if (eof) return -1;
                    if (nfy) {
                        notify();
                        nfy = false;
                    }
                    if (time != 0) {
                        if (cnt > 0) throw new IOException("Time Out");
                        wait(time);
                        cnt++;
                    }
                    else {
                        wait();
                    }
                }
                b[off++] = buf[buf_out];
                buf_out = (buf_out + 1) % buf.length;
                nfy = true;
                res++;
            }
            return res;
        }
        catch (InterruptedException x) {
            throw new InterruptedIOException();
        }
        finally {
            if (nfy) notify();
        }
    }

    public synchronized int available() throws IOException {
        return (buf_inp - buf_out + buf.length) % buf.length;
    }

    public synchronized void close() throws IOException {
        super.close();
        if (thread != null) {
            try {
                thread.interrupt();
                while (!eof) wait();
                thread = null;
            }
            catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    }
}

Back to the top