Commit | Line | Data |
---|---|---|
b732adaa | 1 | /********************************************************************** |
ec619615 | 2 | * Copyright (c) 2014, 2015 Wind River Systems, Inc. and others |
b732adaa MS |
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 | **********************************************************************/ | |
13729cbc BH |
12 | package org.eclipse.tracecompass.internal.tmf.remote.core.shell; |
13 | ||
14 | import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString; | |
b732adaa MS |
15 | |
16 | import java.io.IOException; | |
17 | import java.io.InputStream; | |
18 | import java.io.InputStreamReader; | |
19 | ||
20 | import org.eclipse.core.runtime.IProgressMonitor; | |
c07150f8 | 21 | import org.eclipse.jdt.annotation.NonNullByDefault; |
b732adaa | 22 | |
c07150f8 | 23 | @NonNullByDefault |
b732adaa | 24 | class InputReader { |
65e28a02 MK |
25 | private static final int JOIN_TIMEOUT = 300; |
26 | private static final int BYTES_PER_KB = 1024; | |
ec619615 | 27 | |
b732adaa MS |
28 | private final InputStreamReader fReader; |
29 | private final Thread fThread; | |
30 | private final StringBuilder fResult; | |
31 | private volatile boolean fDone; | |
32 | ||
33 | public InputReader(InputStream inputStream) { | |
34 | fResult = new StringBuilder(); | |
35 | fReader = new InputStreamReader(inputStream); | |
36 | fThread = new Thread() { | |
37 | @Override | |
38 | public void run() { | |
65e28a02 | 39 | final char[] buffer = new char[BYTES_PER_KB]; |
ec619615 | 40 | int read; |
b732adaa | 41 | try { |
ec619615 | 42 | while (!fDone && (read = fReader.read(buffer)) > 0) { |
b732adaa MS |
43 | fResult.append(buffer, 0, read); |
44 | } | |
45 | } catch (IOException e) { | |
46 | } | |
47 | } | |
48 | }; | |
49 | fThread.start(); | |
50 | } | |
51 | ||
52 | public void waitFor(IProgressMonitor monitor) throws InterruptedException { | |
13729cbc | 53 | while (fThread.isAlive() && (!monitor.isCanceled())) { |
65e28a02 | 54 | fThread.join(JOIN_TIMEOUT); |
b732adaa MS |
55 | } |
56 | } | |
57 | ||
58 | public void stop() { | |
59 | fDone = true; | |
60 | fThread.interrupt(); | |
61 | } | |
62 | ||
63 | @Override | |
64 | public String toString() { | |
13729cbc | 65 | return nullToEmptyString(fResult.toString()); |
b732adaa MS |
66 | } |
67 | ||
68 | } |