Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b778ab6f2d624e3aeec599adb2bcc68ea437a539 (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) 2015, 2018 Red Hat.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.core;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import org.eclipse.core.runtime.ListenerList;

// Special Console OutputStream which supports listeners.

public class DockerConsoleOutputStream extends OutputStream {

	private OutputStream stream;
	private Map<String, Object> properties;

	ListenerList<IConsoleListener> consoleListeners;

	public DockerConsoleOutputStream(OutputStream stream) {
		this.stream = stream;
	}

	public DockerConsoleOutputStream setOutputStream(OutputStream stream) {
		this.stream = stream;
		return this;
	}

	public void setTerminalProperties(Map<String, Object> properties) {
		this.properties = properties;
	}

	public Map<String, Object> getTerminalProperties() {
		return properties;
	}

	@Override
	public void write(byte[] b) throws IOException {
		if (stream != null) {
			stream.write(b);
		}
		notifyConsoleListeners(b, 0, b.length);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		if (stream != null) {
			stream.write(b, off, len);
		}
		notifyConsoleListeners(b, off, len);
	}

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

	@Override
	public void close() throws IOException {
		if (stream != null) {
			stream.close();
		}
	}

	@Override
	public void flush() throws IOException {
		if (stream != null) {
			stream.flush();
		}
	}

	public void addConsoleListener(IConsoleListener listener) {
		if (consoleListeners == null)
			consoleListeners = new ListenerList<>(ListenerList.IDENTITY);
		consoleListeners.add(listener);
	}

	public void removeConsoleListener(IConsoleListener listener) {
		if (consoleListeners != null)
			consoleListeners.remove(listener);
	}

	public void notifyConsoleListeners(byte[] b, int off, int len) {
		if (consoleListeners != null) {
			String output = new String(b, off, len);
			Object[] listeners = consoleListeners.getListeners();
			for (int i = 0; i < listeners.length; ++i) {
				((IConsoleListener) listeners[i]).newOutput(output);
			}
		}
	}

}

Back to the top