Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffb0bae224b9c3123c79ab48491389278e5d1df0 (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
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation. 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:
 *    Liping Ke (Intel Corp.) - initial API and implementation
 ******************************************************************************/
package org.eclipse.tm.internal.tcf.rse.shells;

import java.io.IOException;

import java.io.OutputStream;

import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IStreams;
import org.eclipse.tm.tcf.util.TCFTask;

public class TCFTerminalOutputStream extends OutputStream {

    private final IStreams streams;
    private boolean connected = true;
    private boolean write_eof;
    String os_id;

    public TCFTerminalOutputStream(final IStreams streams, final String os_id) throws IOException{
        if (streams == null) throw new IOException("istream is null");//$NON-NLS-1$
        this.streams = streams;
        this.os_id = os_id;
        write_eof = false;
    }

    @Override
    public synchronized void write(final byte b[], final int off, final int len) throws IOException {
        /* If eof is written, we can't write anything into the stream */
        if (!connected || write_eof)
            throw new IOException("stream is not connected or write_eof already!");//$NON-NLS-1$
        try {
            new TCFTask<Object>() {
                public void run() {
                    streams.write(os_id, b, off, len, new IStreams.DoneWrite() {
                        public void doneWrite(IToken token, Exception error) {
                            if (error != null) error(error);
                            done(this);
                        }
                    });

                }
            }.getIO();
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage());//$NON-NLS-1$
        }
    }

    @Override
    public synchronized void write(int b) throws IOException {

        try {
            final byte[] buf = new byte[1];
            buf[0] = (byte)b;
            this.write(buf, 0, 1);
        }
        catch(IOException ioe) {
            throw new IOException(ioe.getMessage());//$NON-NLS-1$
        }

    }

    /* close must be called --Need to reconsider it in the future*/
    public void close() throws IOException {
        if (!connected)
            return;
        try {
            new TCFTask<Object>() {
                public void run() {
                    streams.eos(os_id, new IStreams.DoneEOS() {
                        public void doneEOS(IToken token, Exception error) {
                            write_eof = true;
                            done(this);
                        }
                    });
                }
            }.getIO();
            new TCFTask<Object>() {
                public void run() {
                    streams.disconnect(os_id, new IStreams.DoneDisconnect() {
                        public void doneDisconnect(IToken token, Exception error) {
                            connected = false;
                            done(this);
                        }
                    });

                }
            }.getIO();
        }
        catch(Exception e) {
            throw new IOException(e.getMessage());   //$NON-NLS-1$
        }
    }

}

Back to the top