924e09811e61b33c58264eebacccbe41b59d3691
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.remote.core / src / org / eclipse / tracecompass / internal / tmf / remote / core / shell / InputReader.java
1 /**********************************************************************
2 * Copyright (c) 2014, 2015 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.tmf.remote.core.shell;
13
14 import static org.eclipse.tracecompass.common.core.NonNullUtils.nullToEmptyString;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22
23 @NonNullByDefault
24 class InputReader {
25 private static final int JOIN_TIMEOUT = 300;
26 private static final int BYTES_PER_KB = 1024;
27
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() {
39 final char[] buffer = new char[BYTES_PER_KB];
40 int read;
41 try {
42 while (!fDone && (read = fReader.read(buffer)) > 0) {
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 {
53 while (fThread.isAlive() && (!monitor.isCanceled())) {
54 fThread.join(JOIN_TIMEOUT);
55 }
56 }
57
58 public void stop() {
59 fDone = true;
60 fThread.interrupt();
61 }
62
63 @Override
64 public String toString() {
65 return nullToEmptyString(fResult.toString());
66 }
67
68 }
This page took 0.036285 seconds and 4 git commands to generate.