Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e6d2187fdad49f2a1b8199338e2bb85f79f454eb (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
/*******************************************************************************
 * 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.OutputStream;
import java.util.HashSet;
import java.util.LinkedList;

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

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

    private static final int MAX_QUEUE = 32;

    private final IChannel channel;
    private final IStreams streams;
    private final String id;
    private final boolean send_eos;
    private final Runnable on_close;
    private final byte[] buf = new byte[1];
    private final HashSet<IToken> queue = new HashSet<IToken>();
    private final LinkedList<Exception> errors = new LinkedList<Exception>();
    private final HashSet<Runnable> wait_list = new HashSet<Runnable>();
    private boolean closed;

    public TCFVirtualOutputStream(IChannel channel, String id, boolean send_eos, 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.send_eos = send_eos;
        this.on_close = on_close;
    }

    @Override
    public synchronized void write(final 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;
        new TCFTask<Object>() {
            public void run() {
                if (queue.size() > MAX_QUEUE) {
                    wait_list.add(this);
                    return;
                }
                if (errors.size() > 0) {
                    error(errors.removeFirst());
                    return;
                }
                queue.add(streams.write(id, b, off, len, new IStreams.DoneWrite() {
                    public void doneWrite(IToken token, Exception error) {
                        if (error != null) errors.add(error);
                        queue.remove(token);
                        if (wait_list.size() > 0) {
                            Runnable[] list = wait_list.toArray(new Runnable[wait_list.size()]);
                            wait_list.clear();
                            for (Runnable r : list) r.run();
                        }
                    }
                }));
                done(this);
            }
        }.getIO();
    }

    @Override
    public synchronized void write(int b) throws IOException {
        buf[0] = (byte)b;
        write(buf, 0, 1);
    }

    @Override
    public void flush() throws IOException {
        if (closed) throw new IOException("Stream is closed"); //$NON-NLS-1$
        new TCFTask<Object>() {
            public void run() {
                if (queue.size() > 0) {
                    wait_list.add(this);
                }
                else if (errors.size() > 0) {
                    error(errors.removeFirst());
                }
                else {
                    done(this);
                }
            }
        }.getIO();
    }

    @Override
    public void close() throws IOException {
        flush();
        if (closed) return;
        closed = true;
        if (send_eos) {
            new TCFTask<Object>() {
                public void run() {
                    streams.eos(id, new IStreams.DoneEOS() {
                        public void doneEOS(IToken token, Exception error) {
                            if (error != null && channel.getState() != IChannel.STATE_CLOSED) {
                                error(error);
                            }
                            else {
                                done(this);
                            }
                        }
                    });
                }
            }.getIO();
        }
        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