Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/process/StreamReader.java
41159 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share.process;2324import java.io.InputStream;25import java.io.InputStreamReader;26import java.io.BufferedReader;27import java.io.IOException;28import java.util.List;29import java.util.ArrayList;3031public class StreamReader extends Thread {32private InputStream in;33private List<StreamListener> listeners;34private volatile boolean terminate = false;35private static final long CLEANUP_TIMEOUT = 60000;3637public StreamReader(String desc) {38super("Stream Reader: " + desc);39setDaemon(true);40}4142public StreamReader(String desc, InputStream in) {43this(desc);44setStream(in);45}4647public void setDescription(String desc) {48setName("Stream Reader: " + desc);49}5051public void setStream(InputStream in) {52this.in = in;53}5455public synchronized void addListener(StreamListener listener) {56if (listeners == null)57listeners = new ArrayList<StreamListener>();58listeners.add(listener);59}6061public synchronized void removeListener(StreamListener listener) {62if (listeners != null) {63while (listeners.remove(listener))64;65}66}6768public void run() {69onStart();70BufferedReader rd;71try {72rd = new BufferedReader(new InputStreamReader(in));73String line;74while (!terminate) {75line = rd.readLine();76if (line == null)77break;78onRead(line);79while (rd.ready()) {80line = rd.readLine();81if (line == null)82break;83onRead(line);84}85if (line == null)86break;87}88cleanup();89onFinish();90} catch (IOException e) {91if (!terminate)92onException(e);93else94onFinish();95}96}9798protected void onStart() {99if (listeners != null) {100for (StreamListener l : listeners)101l.onStart();102}103}104105protected void onRead(String line) {106//System.out.println("Read: " + line);107if (listeners != null) {108for (StreamListener l : listeners)109l.onRead(line);110}111}112113protected void onFinish() {114if (listeners != null) {115for (StreamListener l : listeners)116l.onFinish();117}118}119120protected void onException(Throwable e) {121if (listeners != null) {122for (StreamListener l : listeners)123l.onException(e);124}125}126127private void cleanup() {128try {129in.close();130} catch (IOException e) {131e.printStackTrace();132}133}134135public void kill() {136terminate = true;137try {138this.join(CLEANUP_TIMEOUT);139} catch (InterruptedException e) {140e.printStackTrace();141}142this.interrupt();143try {144this.join(CLEANUP_TIMEOUT);145} catch (InterruptedException e) {146e.printStackTrace();147}148}149}150151152