-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
StreamPumper.java
204 lines (181 loc) · 7.07 KB
/
StreamPumper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.test.lib.process;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicBoolean;
public final class StreamPumper implements Runnable {
private static final int BUF_SIZE = 256;
/**
* Pump will be called by the StreamPumper to process the incoming data
*/
public abstract static class Pump {
abstract void register(StreamPumper d);
}
/**
* OutputStream -> Pump adapter
*/
public final static class StreamPump extends Pump {
private final OutputStream out;
public StreamPump(OutputStream out) {
this.out = out;
}
@Override
void register(StreamPumper sp) {
sp.addOutputStream(out);
}
}
/**
* Used to process the incoming data line-by-line
*/
public abstract static class LinePump extends Pump {
@Override
final void register(StreamPumper sp) {
sp.addLineProcessor(this);
}
protected abstract void processLine(String line);
}
private final InputStream in;
private final Set<OutputStream> outStreams = new HashSet<>();
private final Set<LinePump> linePumps = new HashSet<>();
private final AtomicBoolean processing = new AtomicBoolean(false);
public StreamPumper(InputStream in) {
this.in = in;
}
/**
* Create a StreamPumper that reads from in and writes to out.
*
* @param in The stream to read from.
* @param out The stream to write to.
*/
public StreamPumper(InputStream in, OutputStream out) {
this(in);
this.addOutputStream(out);
}
/**
* Implements Thread.run(). Continuously read from {@code in} and write to
* {@code out} until {@code in} has reached end of stream.
* Additionally this method also splits the data read from the buffer into lines,
* and processes each line using linePumps.
* Abort on interruption. Abort on IOExceptions.
*/
@Override
public void run() {
try (BufferedInputStream is = new BufferedInputStream(in);
ByteArrayOutputStream lineBos = new ByteArrayOutputStream()) {
byte[] buf = new byte[BUF_SIZE];
int len = 0;
int linelen = 0;
while ((len = is.read(buf)) > 0 && !Thread.interrupted()) {
for (OutputStream out : outStreams) {
out.write(buf, 0, len);
}
if (!linePumps.isEmpty()) {
int i = 0;
int lastcrlf = -1;
while (i < len) {
if (buf[i] == '\n' || buf[i] == '\r') {
int bufLinelen = i - lastcrlf - 1;
if (bufLinelen > 0) {
lineBos.write(buf, lastcrlf + 1, bufLinelen);
}
linelen += bufLinelen;
if (linelen > 0) {
lineBos.flush();
final String line = lineBos.toString();
linePumps.forEach((lp) -> lp.processLine(line));
lineBos.reset();
linelen = 0;
}
lastcrlf = i;
}
i++;
}
// If no crlf was found, or there was additional data after the last crlf was found, then write the leftover data
// in lineBos. If there is more data to read it will be concatenated with the current data on the next iteration.
// If there is no more data, or no more crlf found, all the remaining data will be processed after the loop, as the
// final line.
if (lastcrlf == -1) {
lineBos.write(buf, 0, len);
linelen += len;
} else if (lastcrlf < len - 1) {
lineBos.write(buf, lastcrlf + 1, len - lastcrlf - 1);
linelen += len - lastcrlf - 1;
}
}
}
// If there was no terminating crlf the remaining data has been written to lineBos,
// but this final line of data now needs to be processed by the linePumper.
final String line = lineBos.toString();
if (!line.isEmpty()) {
linePumps.forEach((lp) -> lp.processLine(line));
}
} catch (IOException e) {
if (!e.getMessage().equalsIgnoreCase("stream closed")) {
e.printStackTrace();
}
} finally {
for (OutputStream out : outStreams) {
try {
out.flush();
} catch (IOException e) {}
}
try {
in.close();
} catch (IOException e) {}
}
}
final void addOutputStream(OutputStream out) {
outStreams.add(out);
}
final void addLineProcessor(LinePump lp) {
linePumps.add(lp);
}
public final StreamPumper addPump(Pump ... pump) {
if (processing.get()) {
throw new IllegalStateException("Can not modify pumper while " +
"processing is in progress");
}
for (Pump p : pump) {
p.register(this);
}
return this;
}
public final Future<Void> process() {
if (!processing.compareAndSet(false, true)) {
throw new IllegalStateException("Can not re-run the processing");
}
FutureTask<Void> result = new FutureTask<>(this, null);
Thread t = new Thread(result);
t.setDaemon(true);
t.start();
return result;
}
}