Java Code Examples for org.xnio.channels.StreamSinkChannel#write()

The following examples show how to use org.xnio.channels.StreamSinkChannel#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringWriteChannelListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void setup(final StreamSinkChannel channel) {
    try {
        int c;
        do {
            c = channel.write(buffer);
        } while (buffer.hasRemaining() && c > 0);
        if (buffer.hasRemaining()) {
            channel.getWriteSetter().set(this);
            channel.resumeWrites();
        } else {
            writeDone(channel);
        }
    } catch (IOException e) {
        handleError(channel, e);
    }
}
 
Example 2
Source File: StringWriteChannelListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void handleEvent(final StreamSinkChannel channel) {
    try {
        int c;
        do {
            c = channel.write(buffer);
        } while (buffer.hasRemaining() && c > 0);
        if (buffer.hasRemaining()) {
            channel.resumeWrites();
            return;
        } else {
            writeDone(channel);
        }
    } catch (IOException e) {
        handleError(channel, e);
    }
}
 
Example 3
Source File: HttpServerExchange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long transferTo(long count, ByteBuffer throughBuffer, StreamSinkChannel target) throws IOException {
    PooledByteBuffer[] buffered = getAttachment(BUFFERED_REQUEST_DATA);
    if (buffered == null) {
        return super.transferTo(count, throughBuffer, target);
    }
    //make sure there is no garbage in throughBuffer
    throughBuffer.position(0);
    throughBuffer.limit(0);
    long copied = 0;
    for (int i = 0; i < buffered.length; ++i) {
        PooledByteBuffer pooled = buffered[i];
        if (pooled != null) {
            final ByteBuffer buf = pooled.getBuffer();
            if (buf.hasRemaining()) {
                int res = target.write(buf);

                if (!buf.hasRemaining()) {
                    pooled.close();
                    buffered[i] = null;
                }
                if (res == 0) {
                    return copied;
                } else {
                    copied += res;
                }
            } else {
                pooled.close();
                buffered[i] = null;
            }
        }
    }
    removeAttachment(BUFFERED_REQUEST_DATA);
    if (copied == 0) {
        return super.transferTo(count, throughBuffer, target);
    } else {
        return copied;
    }
}
 
Example 4
Source File: AbstractFramedStreamSourceChannel.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public long transferTo(long count, ByteBuffer throughBuffer, StreamSinkChannel streamSinkChannel) throws IOException {
    if (anyAreSet(state, STATE_DONE)) {
        return -1;
    }
    beforeRead();
    if (waitingForFrame) {
        throughBuffer.position(throughBuffer.limit());
        return 0;
    }
    try {
        if (frameDataRemaining == 0 && anyAreSet(state, STATE_LAST_FRAME)) {
            synchronized (lock) {
                state |= STATE_RETURNED_MINUS_ONE;
                return -1;
            }
        } else if (data != null && data.getBuffer().hasRemaining()) {
            int old = data.getBuffer().limit();
            try {
                if (count < data.getBuffer().remaining()) {
                    data.getBuffer().limit((int) (data.getBuffer().position() + count));
                }
                int written = streamSinkChannel.write(data.getBuffer());
                if(data.getBuffer().hasRemaining()) {
                    //we can still add more data
                    //stick it it throughbuffer, otherwise transfer code will continue to attempt to use this method
                    throughBuffer.clear();
                    Buffers.copy(throughBuffer, data.getBuffer());
                    throughBuffer.flip();
                } else {
                    throughBuffer.position(throughBuffer.limit());
                }
                return written;
            } finally {
                data.getBuffer().limit(old);
                decrementFrameDataRemaining();
            }
        } else {
            throughBuffer.position(throughBuffer.limit());
        }
        return 0;
    } finally {
        exitRead();
    }
}
 
Example 5
Source File: AsyncSenderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void send(final ByteBuffer buffer, final IoCallback callback) {
    if (callback == null) {
        throw UndertowMessages.MESSAGES.argumentCannotBeNull("callback");
    }
    if(!exchange.getConnection().isOpen()) {
        invokeOnException(callback, new ClosedChannelException());
        return;
    }
    if(exchange.isResponseComplete()) {
        invokeOnException(callback, new IOException(UndertowMessages.MESSAGES.responseComplete()));
    }
    if (this.buffer != null || this.fileChannel != null) {
        throw UndertowMessages.MESSAGES.dataAlreadyQueued();
    }
    long responseContentLength = exchange.getResponseContentLength();
    if(responseContentLength > 0 && buffer.remaining() > responseContentLength) {
        invokeOnException(callback, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(buffer.remaining(), responseContentLength));
        return;
    }
    StreamSinkChannel channel = this.channel;
    if (channel == null) {
        if (callback == IoCallback.END_EXCHANGE) {
            if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) {
                exchange.setResponseContentLength(buffer.remaining());
            }
        }
        this.channel = channel = exchange.getResponseChannel();
        if (channel == null) {
            throw UndertowMessages.MESSAGES.responseChannelAlreadyProvided();
        }
    }
    this.callback = callback;
    if (inCallback) {
        this.buffer = new ByteBuffer[]{buffer};
        return;
    }
    try {
        do {
            if (buffer.remaining() == 0) {
                callback.onComplete(exchange, this);
                return;
            }
            int res = channel.write(buffer);
            if (res == 0) {
                this.buffer = new ByteBuffer[]{buffer};
                this.callback = callback;

                if(writeListener == null) {
                    initWriteListener();
                }
                channel.getWriteSetter().set(writeListener);
                channel.resumeWrites();
                return;
            }
        } while (buffer.hasRemaining());
        invokeOnComplete();

    } catch (IOException e) {

        invokeOnException(callback, e);
    }
}
 
Example 6
Source File: AsyncSenderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void send(final ByteBuffer[] buffer, final IoCallback callback) {
    if (callback == null) {
        throw UndertowMessages.MESSAGES.argumentCannotBeNull("callback");
    }

    if(!exchange.getConnection().isOpen()) {
        invokeOnException(callback, new ClosedChannelException());
        return;
    }
    if(exchange.isResponseComplete()) {
        invokeOnException(callback, new IOException(UndertowMessages.MESSAGES.responseComplete()));
    }
    if (this.buffer != null) {
        throw UndertowMessages.MESSAGES.dataAlreadyQueued();
    }
    this.callback = callback;
    if (inCallback) {
        this.buffer = buffer;
        return;
    }

    long totalToWrite = Buffers.remaining(buffer);
    long responseContentLength = exchange.getResponseContentLength();
    if(responseContentLength > 0 && totalToWrite > responseContentLength) {
        invokeOnException(callback, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(totalToWrite, responseContentLength));
        return;
    }

    StreamSinkChannel channel = this.channel;
    if (channel == null) {
        if (callback == IoCallback.END_EXCHANGE) {
            if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) {
                exchange.setResponseContentLength(totalToWrite);
            }
        }
        this.channel = channel = exchange.getResponseChannel();
        if (channel == null) {
            throw UndertowMessages.MESSAGES.responseChannelAlreadyProvided();
        }
    }

    final long total = totalToWrite;
    long written = 0;

    try {
        do {
            long res = channel.write(buffer);
            written += res;
            if (res == 0) {
                this.buffer = buffer;
                this.callback = callback;

                if(writeListener == null) {
                    initWriteListener();
                }
                channel.getWriteSetter().set(writeListener);
                channel.resumeWrites();
                return;
            }
        } while (written < total);
        invokeOnComplete();

    } catch (IOException e) {
        invokeOnException(callback, e);
    }
}
 
Example 7
Source File: AsyncSenderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Invokes the onComplete method. If send is called again in onComplete then
 * we loop and write it out. This prevents possible stack overflows due to recursion
 */
private void invokeOnComplete() {
    for (; ; ) {
        if (pooledBuffers != null) {
            for (PooledByteBuffer buffer : pooledBuffers) {
                buffer.close();
            }
            pooledBuffers = null;
        }
        IoCallback callback = this.callback;
        this.buffer = null;
        this.fileChannel = null;
        this.callback = null;
        inCallback = true;
        try {
            callback.onComplete(exchange, this);
        } finally {
            inCallback = false;
        }

        StreamSinkChannel channel = this.channel;
        if (this.buffer != null) {
            long t = Buffers.remaining(buffer);
            final long total = t;
            long written = 0;

            try {
                do {
                    long res = channel.write(buffer);
                    written += res;
                    if (res == 0) {
                        if(writeListener == null) {
                            initWriteListener();
                        }
                        channel.getWriteSetter().set(writeListener);
                        channel.resumeWrites();
                        return;
                    }
                } while (written < total);
                //we loop and invoke onComplete again
            } catch (IOException e) {
                invokeOnException(callback, e);
            }
        } else if (this.fileChannel != null) {
            if(transferTask == null) {
                transferTask = new TransferTask();
            }
            if (!transferTask.run(false)) {
                return;
            }
        } else {
            return;
        }

    }
}