/* * Copyright (c) 2013, 2019, 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.
*/
/** * Pump will be called by the StreamPumper to process the incoming data
*/ publicabstractstaticclass Pump { abstractvoid register(StreamPumper d);
}
/** * Used to process the incoming data line-by-line
*/ publicabstractstaticclass LinePump extends Pump {
@Override finalvoid register(StreamPumper sp) {
sp.addLineProcessor(this);
}
protectedabstractvoid processLine(String line);
}
privatefinal InputStream in; privatefinal Set<OutputStream> outStreams = new HashSet<>(); privatefinal Set<LinePump> linePumps = new HashSet<>();
privatefinal 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. Abort on * interruption. Abort on IOExceptions.
*/
@Override publicvoid run() { try (BufferedInputStream is = new BufferedInputStream(in)) {
ByteArrayOutputStream lineBos = new ByteArrayOutputStream(); byte[] buf = newbyte[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 (lastcrlf == -1) {
lineBos.write(buf, 0, len);
linelen += len;
} elseif (lastcrlf < len - 1) {
lineBos.write(buf, lastcrlf + 1, len - lastcrlf - 1);
linelen += len - lastcrlf - 1;
}
}
}
publicfinal StreamPumper addPump(Pump ... pump) { if (processing.get()) { thrownew IllegalStateException("Can not modify pumper while " + "processing is in progress");
} for (Pump p : pump) {
p.register(this);
} returnthis;
}
publicfinal Future<Void> process() { if (!processing.compareAndSet(false, true)) { thrownew IllegalStateException("Can not re-run the processing");
}
FutureTask<Void> result = new FutureTask<>(this, null); Thread t = newThread(result);
t.setDaemon(true);
t.start();
return result;
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.1 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.