1 /**********************************************************************
2 * Copyright (c) 2014, 2015 Wind River Systems, Inc. and others
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
10 * Markus Schorn - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.tracecompass
.internal
.tmf
.remote
.core
.shell
;
14 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.nullToEmptyString
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
18 import java
.io
.InputStreamReader
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
25 private static final int JOIN_TIMEOUT
= 300;
26 private static final int BYTES_PER_KB
= 1024;
28 private final InputStreamReader fReader
;
29 private final Thread fThread
;
30 private final StringBuilder fResult
;
31 private volatile boolean fDone
;
33 public InputReader(InputStream inputStream
) {
34 fResult
= new StringBuilder();
35 fReader
= new InputStreamReader(inputStream
);
36 fThread
= new Thread() {
39 final char[] buffer
= new char[BYTES_PER_KB
];
42 while (!fDone
&& (read
= fReader
.read(buffer
)) > 0) {
43 fResult
.append(buffer
, 0, read
);
45 } catch (IOException e
) {
52 public void waitFor(IProgressMonitor monitor
) throws InterruptedException
{
53 while (fThread
.isAlive() && (!monitor
.isCanceled())) {
54 fThread
.join(JOIN_TIMEOUT
);
64 public String
toString() {
65 return nullToEmptyString(fResult
.toString());