Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bf76ee6a2482b8bdc728a1c5aa38fa71fdc8e68a (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
/*******************************************************************************
 * Copyright (c) 2014 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.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;

import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IStreams;

/**
 * TCFVirtualInputStream is InputStream implementation over TCF Streams service.
 *
 * @since 1.2
 */
public final class TCFVirtualInputStream extends InputStream {

    private static final int MAX_QUEUE = 8;

    private static class Buffer {
        IToken token;
        Exception error;
        byte[] buf;
        int pos;
        boolean eof;
    }

    private final IChannel channel;
    private final IStreams streams;
    private final String id;
    private final Runnable on_close;
    private final LinkedList<Buffer> queue = new LinkedList<Buffer>();
    private Buffer buf;
    private TCFTask<Buffer> task;
    private byte[] tmp = new byte[1];
    private boolean closed;
    private boolean eof;

    public TCFVirtualInputStream(IChannel channel, String id, Runnable on_close) throws IOException {
        this.channel = channel;
        streams = channel.getRemoteService(IStreams.class);
        if (streams == null) throw new IOException("Streams service not available"); //$NON-NLS-1$
        this.id = id;
        this.on_close = on_close;
    }

    @Override
    public synchronized int read(byte b[], final int off, final int len) throws IOException {
        if (closed) throw new IOException("Stream is closed"); //$NON-NLS-1$
        if (b == null) throw new NullPointerException();
        if (off < 0 || off > b.length || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException();
        if (len == 0) return 0;
        try {
            for (;;) {
                if (buf == null) {
                    buf = new TCFTask<Buffer>() {
                        public void run() {
                            while (!eof && queue.size() < MAX_QUEUE) {
                                final Buffer nxt = new Buffer();
                                queue.add(nxt);
                                nxt.token = streams.read(id, 0x10000, new IStreams.DoneRead() {
                                    public void doneRead(IToken token, Exception error, int lost, byte[] data, boolean eos) {
                                        assert nxt.token == token;
                                        nxt.token = null;
                                        nxt.error = error;
                                        nxt.buf = data;
                                        nxt.eof = eos;
                                        if (!eof && (eos || error != null)) eof = true;
                                        if (task != null) {
                                            assert queue.getFirst() == nxt;
                                            task.done(queue.removeFirst());
                                            task = null;
                                        }
                                    }
                                });
                            }
                            if (queue.getFirst().token == null) {
                                done(queue.removeFirst());
                            }
                            else {
                                task = this;
                            }
                        }
                    }.getIO();
                }
                if (buf.buf != null && buf.pos < buf.buf.length) {
                    int n = len;
                    if (n > buf.buf.length - buf.pos) n = buf.buf.length - buf.pos;
                    System.arraycopy(buf.buf, buf.pos, b, off, n);
                    buf.pos += n;
                    return n;
                }
                if (buf.error instanceof IOException) throw (IOException)buf.error;
                if (buf.error != null) throw new IOException(buf.error);
                if (buf.eof) return -1;
                buf = null;
            }
        }
        catch (IOException e) {
            if (closed) return -1;
            throw e;
        }
    }

    @Override
    public synchronized int read() throws IOException {
        if (!closed && buf != null && buf.buf != null && buf.pos < buf.buf.length) {
            return buf.buf[buf.pos++] & 0xff;
        }
        int n = read(tmp, 0, 1);
        if (n < 0) return -1;
        assert n == 1;
        return tmp[0] & 0xff;
    }

    @Override
    public void close() throws IOException {
        if (closed) return;
        closed = true;
        new TCFTask<Object>() {
            public void run() {
                streams.disconnect(id, new IStreams.DoneDisconnect() {
                    public void doneDisconnect(IToken token, Exception error) {
                        if (error != null && channel.getState() != IChannel.STATE_CLOSED) {
                            error(error);
                        }
                        else {
                            if (on_close != null) on_close.run();
                            done(this);
                        }
                    }
                });
            }
        }.getIO();
    }
}

Back to the top