use of org.xnio.channels.EmptyStreamSourceChannel in project camel by apache.
the class DefaultUndertowHttpBindingTest method source.
private StreamSourceChannel source(final String[] delayedPayloads) {
Thread sourceThread = Thread.currentThread();
return new EmptyStreamSourceChannel(thread()) {
int chunk;
// make sure that the caller is not spinning on read==0
boolean mustWait;
@Override
public int read(ByteBuffer dst) throws IOException {
if (mustWait) {
fail("must wait before reading");
}
if (chunk < delayedPayloads.length) {
byte[] delayedPayload = delayedPayloads[chunk].getBytes();
dst.put(delayedPayload);
chunk++;
if (delayedPayload.length == 0) {
mustWait = true;
}
return delayedPayload.length;
}
return -1;
}
@Override
public void resumeReads() {
/**
* {@link io.undertow.server.HttpServerExchange.ReadDispatchChannel} delays resumes in the main thread
*/
if (sourceThread != Thread.currentThread()) {
super.resumeReads();
}
}
@Override
public void awaitReadable() throws IOException {
mustWait = false;
super.awaitReadable();
}
};
}
Aggregations