Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/process/StreamMessageInput.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.util.List;
26
import java.util.ArrayList;
27
import nsk.share.TestBug;
28
29
public class StreamMessageInput implements MessageInput {
30
private Object sync = new Object();
31
private List<String> lines = new ArrayList<String>();
32
private int position = 0;
33
private volatile boolean active = false;
34
private volatile Throwable exception;
35
36
public StreamMessageInput() {
37
}
38
39
public StreamMessageInput(StreamReader sd) {
40
bind(sd);
41
}
42
43
public StreamListener createListener() {
44
return new Listener();
45
}
46
47
public void bind(StreamReader sd) {
48
sd.addListener(createListener());
49
}
50
51
public boolean isActive() {
52
return active;
53
}
54
55
public boolean isException() {
56
return exception != null;
57
}
58
59
public Throwable getException() {
60
return exception;
61
}
62
63
public boolean waitForStart(long timeout) throws InterruptedException {
64
long startTime = System.currentTimeMillis();
65
long curTime = startTime;
66
synchronized (sync) {
67
while (!active && curTime - startTime < timeout) {
68
sync.wait(curTime - startTime);
69
curTime = System.currentTimeMillis();
70
}
71
}
72
return active;
73
}
74
75
public boolean waitForFinish(long timeout) throws InterruptedException {
76
long startTime = System.currentTimeMillis();
77
long curTime = startTime;
78
synchronized (sync) {
79
while (active && curTime - startTime < timeout) {
80
sync.wait(curTime - startTime);
81
curTime = System.currentTimeMillis();
82
}
83
}
84
return !active;
85
}
86
87
public boolean waitForMessage(String msg, long timeout) throws InterruptedException {
88
long startTime = System.currentTimeMillis();
89
long curTime = startTime;
90
int n = position;
91
synchronized (sync) {
92
while (curTime - startTime < timeout) {
93
while (n < lines.size()) {
94
// System.out.println("Check: " + lines.get(n));
95
if (msg == null || lines.get(n++).contains(msg)) {
96
return true;
97
}
98
}
99
sync.wait(timeout - (curTime - startTime));
100
curTime = System.currentTimeMillis();
101
}
102
return false;
103
}
104
}
105
106
public boolean waitForMessage(long timeout) throws InterruptedException {
107
return waitForMessage(null, timeout);
108
}
109
110
public String getMessage() {
111
if (position < lines.size())
112
return lines.get(position++);
113
else
114
return null;
115
}
116
117
public String getMessage(int index) {
118
return lines.get(index);
119
}
120
121
public int getPosition() {
122
return position;
123
}
124
125
public void setPosition(int position) {
126
this.position = position;
127
}
128
129
public int getMessageCount() {
130
return lines.size();
131
}
132
133
public List<String> getMessages() {
134
return getMessages(position, lines.size());
135
}
136
137
public List<String> getMessages(int to) {
138
return getMessages(position, to);
139
}
140
141
public List<String> getMessages(int from, int to) {
142
synchronized (sync) {
143
if (to < 0)
144
to = lines.size() + to;
145
position = Math.max(position, to);
146
return new ArrayList<String>(lines.subList(from, to));
147
}
148
}
149
150
public void reset() {
151
synchronized (sync) {
152
position = lines.size();
153
}
154
}
155
156
private class Listener implements StreamListener {
157
@Override
158
public void onStart() {
159
synchronized (sync) {
160
active = true;
161
sync.notifyAll();
162
}
163
}
164
165
@Override
166
public void onRead(String line) {
167
//System.out.println("onRead: " + line);
168
synchronized (sync) {
169
lines.add(line);
170
sync.notifyAll();
171
}
172
}
173
174
@Override
175
public void onFinish() {
176
synchronized (sync) {
177
active = false;
178
sync.notifyAll();
179
}
180
}
181
182
@Override
183
public void onException(Throwable e) {
184
exception = e;
185
}
186
}
187
}
188
189