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

The following examples show how to use org.xnio.channels.StreamSinkChannel#resumeWrites() . 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: UndertowServerHttpResponse.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public void transfer(StreamSinkChannel destination) {
	try {
		while (this.count > 0) {
			long len = destination.transferFrom(this.source, this.position, this.count);
			if (len != 0) {
				this.position += len;
				this.count -= len;
			}
			else {
				destination.resumeWrites();
				return;
			}
		}
		this.sink.success();
	}
	catch (IOException ex) {
		this.sink.error(ex);
	}

}
 
Example 2
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 3
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 4
Source File: StringWriteChannelListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void writeDone(final StreamSinkChannel channel) {
    try {
        channel.shutdownWrites();

        if (!channel.flush()) {
            channel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {
                @Override
                public void handleEvent(StreamSinkChannel o) {
                    IoUtils.safeClose(channel);
                }
            }, ChannelListeners.closingChannelExceptionHandler()));
            channel.resumeWrites();

        }
    } catch (IOException e) {
        handleError(channel, e);
    }
}
 
Example 5
Source File: UndertowServerHttpResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected boolean isWritePossible() {
	StreamSinkChannel channel = UndertowServerHttpResponse.this.responseChannel;
	if (channel != null) {
		// We can always call flush, just ensure writes are on..
		channel.resumeWrites();
		return true;
	}
	return false;
}
 
Example 6
Source File: UndertowServerHttpResponse.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected boolean isWritePossible() {
	StreamSinkChannel channel = UndertowServerHttpResponse.this.responseChannel;
	if (channel != null) {
		// We can always call flush, just ensure writes are on..
		channel.resumeWrites();
		return true;
	}
	return false;
}
 
Example 7
Source File: Http2Channel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void flushChannel(StreamSinkChannel stream) throws IOException {
    stream.shutdownWrites();
    if (!stream.flush()) {
        stream.getWriteSetter().set(ChannelListeners.flushingChannelListener(null, writeExceptionHandler()));
        stream.resumeWrites();
    }
}
 
Example 8
Source File: AsyncSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public boolean run(boolean complete) {
    try {
        FileChannel source = fileChannel;
        long pos = source.position();
        long size = source.size();

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

        while (size - pos > 0) {
            long ret = dest.transferFrom(source, pos, size - pos);
            pos += ret;
            if (ret == 0) {
                source.position(pos);
                dest.getWriteSetter().set(this);
                dest.resumeWrites();
                return false;
            }
        }

        if (complete) {
            invokeOnComplete();
        }
    } catch (IOException e) {
        invokeOnException(callback, e);
    }

    return true;
}
 
Example 9
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 10
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 11
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;
        }

    }
}