Path: blob/master/test/hotspot/jtreg/serviceability/jdwp/StreamHandler.java
41149 views
/*1* Copyright (c) 2016, 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*/2223import java.io.BufferedReader;24import java.io.IOException;25import java.io.InputStream;26import java.io.InputStreamReader;27import java.util.concurrent.ExecutorService;28import java.util.concurrent.Executors;2930/**31* Handles the process output (either stdin or stdout)32* passing the lines to a listener33*/34public class StreamHandler implements Runnable {3536public interface Listener {37/**38* Called when a line has been read from the process output stream39* @param handler this StreamHandler40* @param s the line41*/42void onStringRead(StreamHandler handler, String s);43}4445private final ExecutorService executor;46private final InputStream is;47private final Listener listener;4849/**50* @param is input stream to read from51* @param listener listener to pass the read lines to52* @throws IOException53*/54public StreamHandler(InputStream is, Listener listener) throws IOException {55this.is = is;56this.listener = listener;57executor = Executors.newSingleThreadExecutor();58}5960/**61* Starts asynchronous reading62*/63public void start() {64executor.submit(this);65}6667@Override68public void run() {69try {70BufferedReader br = new BufferedReader(new InputStreamReader(is));71String line;72while ((line = br.readLine()) != null) {73listener.onStringRead(this, line);74}75} catch (Exception x) {76throw new RuntimeException(x);77}78}7980}818283