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
|
/*******************************************************************************
* Copyright (c) 2015, 2016 Red Hat.
* 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:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.linuxtools.internal.docker.ui.consoles;
import java.io.IOException;
import java.io.OutputStream;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.linuxtools.docker.ui.launch.IRunConsoleListener;
// Special Console OutputStream which supports listeners.
public class ConsoleOutputStream extends OutputStream {
private OutputStream stream;
ListenerList<IRunConsoleListener> consoleListeners;
public ConsoleOutputStream(OutputStream stream) {
this.stream = stream;
}
@Override
public void write(byte[] b) throws IOException {
stream.write(b);
notifyConsoleListeners(b, 0, b.length);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
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 {
stream.close();
}
@Override
public void flush() throws IOException {
stream.flush();
}
public void addConsoleListener(IRunConsoleListener listener) {
if (consoleListeners == null)
consoleListeners = new ListenerList<>(ListenerList.IDENTITY);
consoleListeners.add(listener);
}
public void removeConsoleListener(IRunConsoleListener 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) {
((IRunConsoleListener) listeners[i]).newOutput(output);
}
}
}
}
|