Commit | Line | Data |
---|---|---|
b732adaa MS |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 Wind River Systems, Inc. and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Markus Schorn - Initial API and implementation | |
11 | **********************************************************************/ | |
12 | package org.eclipse.tracecompass.internal.lttng2.control.ui.views.remote; | |
13 | ||
14 | import java.io.IOException; | |
15 | import java.io.InputStream; | |
16 | import java.io.InputStreamReader; | |
17 | ||
18 | import org.eclipse.core.runtime.IProgressMonitor; | |
19 | ||
20 | class InputReader { | |
21 | private final InputStreamReader fReader; | |
22 | private final Thread fThread; | |
23 | private final StringBuilder fResult; | |
24 | private volatile boolean fDone; | |
25 | ||
26 | public InputReader(InputStream inputStream) { | |
27 | fResult = new StringBuilder(); | |
28 | fReader = new InputStreamReader(inputStream); | |
29 | fThread = new Thread() { | |
30 | @Override | |
31 | public void run() { | |
32 | final char[] buffer = new char[1024]; | |
33 | int read; | |
34 | try { | |
35 | while (!fDone && (read = fReader.read(buffer)) > 0) { | |
36 | fResult.append(buffer, 0, read); | |
37 | } | |
38 | } catch (IOException e) { | |
39 | } | |
40 | } | |
41 | }; | |
42 | fThread.start(); | |
43 | } | |
44 | ||
45 | public void waitFor(IProgressMonitor monitor) throws InterruptedException { | |
46 | while (fThread.isAlive() && (monitor == null || !monitor.isCanceled())) { | |
47 | fThread.join(300); | |
48 | } | |
49 | } | |
50 | ||
51 | public void stop() { | |
52 | fDone = true; | |
53 | fThread.interrupt(); | |
54 | } | |
55 | ||
56 | @Override | |
57 | public String toString() { | |
58 | return fResult.toString(); | |
59 | } | |
60 | ||
61 | } |