Search in sources :

Example 1 with EmptyStreamSourceChannel

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();
        }
    };
}
Also used : EmptyStreamSourceChannel(org.xnio.channels.EmptyStreamSourceChannel) ByteBuffer(java.nio.ByteBuffer) XnioIoThread(org.xnio.XnioIoThread)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 XnioIoThread (org.xnio.XnioIoThread)1 EmptyStreamSourceChannel (org.xnio.channels.EmptyStreamSourceChannel)1