Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jdwp/StreamHandler.java
41149 views
1
/*
2
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.BufferedReader;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.InputStreamReader;
28
import java.util.concurrent.ExecutorService;
29
import java.util.concurrent.Executors;
30
31
/**
32
* Handles the process output (either stdin or stdout)
33
* passing the lines to a listener
34
*/
35
public class StreamHandler implements Runnable {
36
37
public interface Listener {
38
/**
39
* Called when a line has been read from the process output stream
40
* @param handler this StreamHandler
41
* @param s the line
42
*/
43
void onStringRead(StreamHandler handler, String s);
44
}
45
46
private final ExecutorService executor;
47
private final InputStream is;
48
private final Listener listener;
49
50
/**
51
* @param is input stream to read from
52
* @param listener listener to pass the read lines to
53
* @throws IOException
54
*/
55
public StreamHandler(InputStream is, Listener listener) throws IOException {
56
this.is = is;
57
this.listener = listener;
58
executor = Executors.newSingleThreadExecutor();
59
}
60
61
/**
62
* Starts asynchronous reading
63
*/
64
public void start() {
65
executor.submit(this);
66
}
67
68
@Override
69
public void run() {
70
try {
71
BufferedReader br = new BufferedReader(new InputStreamReader(is));
72
String line;
73
while ((line = br.readLine()) != null) {
74
listener.onStringRead(this, line);
75
}
76
} catch (Exception x) {
77
throw new RuntimeException(x);
78
}
79
}
80
81
}
82
83