Java Code Examples for io.undertow.io.IoCallback#onException()

The following examples show how to use io.undertow.io.IoCallback#onException() . 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: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private boolean writeBuffer(final ByteBuffer buffer, final IoCallback callback) {
    StringBuilder builder = new StringBuilder();
    try {
        builder.append(charsetDecoder.decode(buffer));
    } catch (CharacterCodingException e) {
        callback.onException(exchange, this, e);
        return false;
    }
    String data = builder.toString();
    writer.write(data);
    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
        return false;
    }
    return true;
}
 
Example 2
Source File: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void queue(final ByteBuffer[] byteBuffers, final IoCallback ioCallback) {
    //if data is sent from withing the callback we queue it, to prevent the stack growing indefinitely
    if (next != null || pendingFile != null) {
        throw UndertowMessages.MESSAGES.dataAlreadyQueued();
    }
    StringBuilder builder = new StringBuilder();
    for (ByteBuffer buffer : byteBuffers) {
        try {
            builder.append(charsetDecoder.decode(buffer));
        } catch (CharacterCodingException e) {
            ioCallback.onException(exchange, this, e);
            return;
        }
    }
    this.next = builder.toString();
    queuedCallback = ioCallback;
}
 
Example 3
Source File: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void send(final String data, final IoCallback callback) {
    if (inCall) {
        queue(data, callback);
        return;
    }
    writer.write(data);

    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
    } else {
        invokeOnComplete(callback);
    }
}
 
Example 4
Source File: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void send(final String data, final Charset charset, final IoCallback callback) {
    if (inCall) {
        queue(new ByteBuffer[]{ByteBuffer.wrap(data.getBytes(charset))}, callback);
        return;
    }
    writer.write(data);
    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
    } else {
        invokeOnComplete(callback);
    }
}
 
Example 5
Source File: BlockingWriterSenderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void performTransfer(FileChannel source, IoCallback callback) {

        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
        try {
            long pos = source.position();
            long size = source.size();
            while (size - pos > 0) {
                int ret = source.read(buffer);
                if (ret <= 0) {
                    break;
                }
                pos += ret;
                buffer.flip();
                if (!writeBuffer(buffer, callback)) {
                    return;
                }
                buffer.clear();
            }

            if (pos != size) {
                throw new EOFException("Unexpected EOF reading file");
            }

        } catch (IOException e) {
            callback.onException(exchange, this, e);
        }
        invokeOnComplete(callback);
    }
 
Example 6
Source File: HttpContinue.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sends a continuation using async IO, and calls back when it is complete.
 *
 * @param exchange The exchange
 * @param callback The completion callback
 */
public static void sendContinueResponse(final HttpServerExchange exchange, final IoCallback callback) {
    if (!exchange.isResponseChannelAvailable()) {
        callback.onException(exchange, null, UndertowMessages.MESSAGES.cannotSendContinueResponse());
        return;
    }
    internalSendContinueResponse(exchange, callback);
}