Skip to main content
summaryrefslogtreecommitdiffstats
blob: 106adc3afbe9904bf6e851b6d7385b7655b9d6dd (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
/*******************************************************************************
 * Copyright (c) 2007, 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 - Initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.jarprocessor;

import java.io.*;

public class StreamProcessor {
	private static final String JOBS = "org.eclipse.core.runtime.jobs.Job"; //$NON-NLS-1$
	public static final String STREAM_PROCESSOR = "Stream Processor"; //$NON-NLS-1$
	public static final String STDERR = "STDERR"; //$NON-NLS-1$
	public static final String STDOUT = "STDOUT"; //$NON-NLS-1$

	static private boolean haveJobs = false;

	static {
		try {
			haveJobs = (Class.forName(JOBS) != null);
		} catch (ClassNotFoundException e) {
			//no jobs
		}
	}

	static public void start(final InputStream is, final String name, final boolean verbose) {
		if (haveJobs) {
			new StreamProcessorJob(is, name, verbose).schedule();
		} else {
			Thread job = new Thread(STREAM_PROCESSOR) {
				@Override
				public void run() {
					StreamProcessor.run(is, name, verbose);
				}
			};
			job.start();
		}
	}

	static public void run(InputStream inputStream, String name, boolean verbose) {
		try {
			InputStreamReader isr = new InputStreamReader(inputStream);
			BufferedReader br = new BufferedReader(isr);
			while (true) {
				String s = br.readLine();
				if (s == null) {
					break;
				}
				if (verbose) {
					if (STDERR.equals(name))
						System.err.println(name + ": " + s); //$NON-NLS-1$
					else
						System.out.println(name + ": " + s); //$NON-NLS-1$
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Back to the top