Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc665412927f5ce3f0a4b295e0abb621080188b8 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Patrik Suzzi <psuzzi@gmail.com> - Bug 507661
 *******************************************************************************/
package org.eclipse.ui.console;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.WorkbenchEncoding;
import org.eclipse.ui.internal.console.IOConsolePartitioner;
import org.eclipse.ui.internal.console.StreamDecoder;

/**
 * OutputStream used to write to an IOConsole.
 * <p>
 * Clients are not intended to instantiate this class directly, instead
 * use <code>IOConsole.newOutputStream()</code>.
 * </p>
 * <p>
 * Clients should avoid writing large amounts of output to this stream in the UI
 * thread. The console needs to process the output in the UI thread and if the client
 * hogs the UI thread writing output to the console, the console will not be able
 * to process the output.
 * </p>
 * @since 3.1
 * @noinstantiate This class is not intended to be instantiated by clients.
 * @noextend This class is not intended to be subclassed by clients.
 */
public class IOConsoleOutputStream extends OutputStream {
	/**
	 * Flag indicating whether this stream has been closed.
	 */
	private boolean closed = false;

	/**
	 * The console's document partitioner.
	 */
	private IOConsolePartitioner partitioner;

	/**
	 * The console this stream is attached to.
	 */
	private IOConsole console;

	/**
	 * Flag indicating that the console should be activated when data
	 * is written to this stream.
	 */
	private boolean activateOnWrite = false;

	/**
	 * The color used to decorate data written to this stream.
	 */
	private Color color;

	/**
	 * The font style used to decorate data written to this stream.
	 */
	private int fontStyle;

	private StreamDecoder decoder;

	private boolean prependCR;

	/**
	 * Constructs a new output stream on the given console.
	 *
	 * @param console I/O console
	 */
	IOConsoleOutputStream(IOConsole console, Charset charset) {
		this.decoder = new StreamDecoder(charset);
		this.console = console;
		this.partitioner = (IOConsolePartitioner) console.getPartitioner();
	}

	/**
	 * Returns the font style used to decorate data written to this stream.
	 *
	 * @return the font style used to decorate data written to this stream
	 */
	public int getFontStyle() {
		return fontStyle;
	}

	/**
	 * Sets the font style to be used to decorate data written to this stream.
	 *
	 * @param newFontStyle the font style to be used to decorate data written to this stream
	 */
	public void setFontStyle(int newFontStyle) {
		if (newFontStyle != fontStyle) {
			int old = fontStyle;
			fontStyle = newFontStyle;
			console.firePropertyChange(this, IConsoleConstants.P_FONT_STYLE, Integer.valueOf(old), Integer.valueOf(fontStyle));
		}
	}

	/**
	 * Returns whether the console this stream is writing to will be activated when this stream
	 * is written to.
	 *
	 * @return whether the console this stream is writing to will be activated when this stream
	 * is written to.
	 */
	public boolean isActivateOnWrite() {
		return activateOnWrite;
	}

	/**
	 * Sets whether to activate the console this stream is writing to when this stream
	 * is written to.
	 *
	 * @param activateOnWrite whether the console this stream is writing to will be activated when this stream
	 * is written to.
	 */
	public void setActivateOnWrite(boolean activateOnWrite) {
		this.activateOnWrite = activateOnWrite;
	}

	/**
	 * Sets the color of this stream. Use <code>null</code> to indicate
	 * the default color.
	 *
	 * @param newColor color of this stream, or <code>null</code>
	 */
	public void setColor(Color newColor) {
		Color old = color;
		if (old == null || !old.equals(newColor)) {
			color = newColor;
			console.firePropertyChange(this, IConsoleConstants.P_STREAM_COLOR, old, newColor);
		}
	}

	/**
	 * Returns the color of this stream, or <code>null</code>
	 * if default.
	 *
	 * @return the color of this stream, or <code>null</code>
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * Returns true if the stream has been closed
	 * @return true is the stream has been closed, false otherwise.
	 */
	public synchronized boolean isClosed() {
		return closed;
	}

	/*
	 *  (non-Javadoc)
	 * @see java.io.OutputStream#close()
	 */
	@Override
	public synchronized void close() throws IOException {
		if(closed) {
			// Closeable#close() has no effect if already closed
			return;
		}
		StringBuilder builder = new StringBuilder();
		if (prependCR) { // force writing of last /r
			prependCR = false;
			builder.append('\r');
		}
		this.decoder.finish(builder);
		if (builder.length() > 0) {
			notifyParitioner(builder.toString());
		}
		console.streamClosed(this);
		closed = true;
		partitioner = null;
		decoder = null;
	}

	/*
	 *  (non-Javadoc)
	 * @see java.io.OutputStream#flush()
	 */
	@Override
	public void flush() throws IOException {
		if(closed) {
			throw new IOException("Output Stream is closed"); //$NON-NLS-1$
		}
	}

	/*
	 *  (non-Javadoc)
	 * @see java.io.OutputStream#write(byte[], int, int)
	 */
	@Override
	public synchronized void write(byte[] b, int off, int len) throws IOException {
		if (closed) {
			throw new IOException("Output Stream is closed"); //$NON-NLS-1$
		}
		StringBuilder builder = new StringBuilder();
		this.decoder.decode(builder, b, off, len);
		encodedWrite(builder.toString());
	}
	/*
	 *  (non-Javadoc)
	 * @see java.io.OutputStream#write(byte[])
	 */
	@Override
	public void write(byte[] b) throws IOException {
		write(b, 0, b.length);
	}
	/*
	 *  (non-Javadoc)
	 * @see java.io.OutputStream#write(int)
	 */
	@Override
	public void write(int b) throws IOException {
		write(new byte[] {(byte)b}, 0, 1);
	}

	/**
	 * Writes a character array to the attached console.
	 *
	 * @param buffer the char array to write to the attached console
	 * @throws IOException if the stream is closed
	 * @since 3.7
	 */
	public void write(char[] buffer) throws IOException {
		String str = new String(buffer);
		this.encodedWrite(str);
	}

	/**
	 * Writes a character array using specified offset and length to the
	 * attached console.
	 *
	 * @param buffer the char array to write to the attached console.
	 * @param off the initial offset
	 * @param len the length
	 * @throws IOException if the stream is closed
	 * @since 3.7
	 */
	public void write(char[] buffer, int off, int len) throws IOException {
		String str = new String(buffer, off, len);
		this.encodedWrite(str);
	}

	/**
	 * Writes a character sequence to the attached console.
	 *
	 * @param chars the string/characters to write to the attached console.
	 * @throws IOException if the stream is closed.
	 * @since 3.7
	 */
	public void write(CharSequence chars) throws IOException {
		String str = chars.toString();
		encodedWrite(str);
	}

	/**
	 * Writes a string to the attached console.
	 *
	 * @param str the string to write to the attached console
	 * @throws IOException if the stream is closed
	 */
	public void write(String str) throws IOException {
		encodedWrite(str);
	}

	private synchronized void encodedWrite(String encodedString) throws IOException {
		if(closed) {
			throw new IOException("Output Stream is closed"); //$NON-NLS-1$
		}
		String newencoding = encodedString;
		if (prependCR){
			newencoding = "\r" + newencoding; //$NON-NLS-1$
			prependCR=false;
		}
		if (newencoding.endsWith("\r")) { //$NON-NLS-1$
			prependCR = true;
			newencoding = new String(newencoding.substring(0, newencoding.length() - 1));
		}
		notifyParitioner(newencoding);
	}

	private void notifyParitioner(String encodedString) throws IOException {
		try {
			partitioner.streamAppended(this, encodedString);

			if (activateOnWrite) {
				console.activate();
			} else {
				ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(console);
			}
		} catch (IOException e) {
			if (!closed) {
				close();
			}
			throw e;
		}
	}

	/**
	 * Sets the character encoding used to interpret characters written to this steam.
	 *
	 * @param encoding encoding identifier
	 */
	public void setEncoding(String encoding) {
		String charsetName;
		if (encoding == null) {
			charsetName = WorkbenchEncoding.getWorkbenchDefaultEncoding();
		} else {
			charsetName = encoding;
		}
		Charset charset = Charset.forName(charsetName);
		try {
			this.setCharset(charset);
		} catch (IOException ioe) {
			// ignore exception while writing final characters
			// to avoid API break
		}
	}

	/**
	 * @param charset set the Charset for the attached console
	 * @throws IOException if the stream is closed
	 * @since 3.7
	 */
	public synchronized void setCharset(Charset charset) throws IOException {
		if (closed) {
			throw new IOException("Output Stream is closed"); //$NON-NLS-1$
		}
		StringBuilder builder = new StringBuilder();
		this.decoder.finish(builder);
		if (builder.length() > 0) {
			this.encodedWrite(builder.toString());
		}
		this.decoder = new StreamDecoder(charset);
	}

}

Back to the top