Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/process/StreamReader.java
41159 views
1
/*
2
* Copyright (c) 2011, 2018, 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
package vm.share.process;
24
25
import java.io.InputStream;
26
import java.io.InputStreamReader;
27
import java.io.BufferedReader;
28
import java.io.IOException;
29
import java.util.List;
30
import java.util.ArrayList;
31
32
public class StreamReader extends Thread {
33
private InputStream in;
34
private List<StreamListener> listeners;
35
private volatile boolean terminate = false;
36
private static final long CLEANUP_TIMEOUT = 60000;
37
38
public StreamReader(String desc) {
39
super("Stream Reader: " + desc);
40
setDaemon(true);
41
}
42
43
public StreamReader(String desc, InputStream in) {
44
this(desc);
45
setStream(in);
46
}
47
48
public void setDescription(String desc) {
49
setName("Stream Reader: " + desc);
50
}
51
52
public void setStream(InputStream in) {
53
this.in = in;
54
}
55
56
public synchronized void addListener(StreamListener listener) {
57
if (listeners == null)
58
listeners = new ArrayList<StreamListener>();
59
listeners.add(listener);
60
}
61
62
public synchronized void removeListener(StreamListener listener) {
63
if (listeners != null) {
64
while (listeners.remove(listener))
65
;
66
}
67
}
68
69
public void run() {
70
onStart();
71
BufferedReader rd;
72
try {
73
rd = new BufferedReader(new InputStreamReader(in));
74
String line;
75
while (!terminate) {
76
line = rd.readLine();
77
if (line == null)
78
break;
79
onRead(line);
80
while (rd.ready()) {
81
line = rd.readLine();
82
if (line == null)
83
break;
84
onRead(line);
85
}
86
if (line == null)
87
break;
88
}
89
cleanup();
90
onFinish();
91
} catch (IOException e) {
92
if (!terminate)
93
onException(e);
94
else
95
onFinish();
96
}
97
}
98
99
protected void onStart() {
100
if (listeners != null) {
101
for (StreamListener l : listeners)
102
l.onStart();
103
}
104
}
105
106
protected void onRead(String line) {
107
//System.out.println("Read: " + line);
108
if (listeners != null) {
109
for (StreamListener l : listeners)
110
l.onRead(line);
111
}
112
}
113
114
protected void onFinish() {
115
if (listeners != null) {
116
for (StreamListener l : listeners)
117
l.onFinish();
118
}
119
}
120
121
protected void onException(Throwable e) {
122
if (listeners != null) {
123
for (StreamListener l : listeners)
124
l.onException(e);
125
}
126
}
127
128
private void cleanup() {
129
try {
130
in.close();
131
} catch (IOException e) {
132
e.printStackTrace();
133
}
134
}
135
136
public void kill() {
137
terminate = true;
138
try {
139
this.join(CLEANUP_TIMEOUT);
140
} catch (InterruptedException e) {
141
e.printStackTrace();
142
}
143
this.interrupt();
144
try {
145
this.join(CLEANUP_TIMEOUT);
146
} catch (InterruptedException e) {
147
e.printStackTrace();
148
}
149
}
150
}
151
152