Search in sources :

Example 1 with BlockingReadableByteChannel

use of org.xnio.channels.BlockingReadableByteChannel in project camel by apache.

the class DefaultUndertowHttpBinding method readFromChannel.

byte[] readFromChannel(StreamSourceChannel source) throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteBuffer buffer = ByteBuffer.wrap(new byte[1024]);
    ReadableByteChannel blockingSource = new BlockingReadableByteChannel(source);
    for (; ; ) {
        int res = blockingSource.read(buffer);
        if (res == -1) {
            return out.toByteArray();
        } else if (res == 0) {
            LOG.error("Channel did not block");
        } else {
            buffer.flip();
            out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.arrayOffset() + buffer.limit());
            buffer.clear();
        }
    }
}
Also used : BlockingReadableByteChannel(org.xnio.channels.BlockingReadableByteChannel) BlockingReadableByteChannel(org.xnio.channels.BlockingReadableByteChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 BlockingReadableByteChannel (org.xnio.channels.BlockingReadableByteChannel)1