@Override public Sender getSender() { if (sender == null) { sender = new BlockingSenderImpl(exchange, getOutputStream()); } return sender; }
@Override public void send(final ByteBuffer buffer, final IoCallback callback) { if (inCall) { queue(new ByteBuffer[]{buffer}, callback); return; } else { long responseContentLength = exchange.getResponseContentLength(); if(responseContentLength > 0 && buffer.remaining() > responseContentLength) { callback.onException(exchange, this, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(buffer.remaining(), responseContentLength)); return; } if (!exchange.isResponseStarted() && callback == IoCallback.END_EXCHANGE) { if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.setResponseContentLength(buffer.remaining()); } } } if (writeBuffer(buffer, callback)) { invokeOnComplete(callback); } }
@Override public void transferFrom(FileChannel source, IoCallback callback) { if (inCall) { queue(source, callback); return; } performTransfer(source, callback); invokeOnComplete(callback); }
private void invokeOnComplete(final IoCallback callback) { inCall = true; try { callback.onComplete(exchange, this); } finally { inCall = false; } while (next != null || pendingFile != null) { ByteBuffer[] next = this.next; IoCallback queuedCallback = this.queuedCallback; FileChannel file = this.pendingFile; this.next = null; this.queuedCallback = null; this.pendingFile = null; if (next != null) { for (ByteBuffer buffer : next) { writeBuffer(buffer, queuedCallback); } } else if (file != null) { performTransfer(file, queuedCallback); } inCall = true; try { queuedCallback.onComplete(exchange, this); } finally { inCall = false; } } }
@Override public void send(final String data, final IoCallback callback) { byte[] bytes = data.getBytes(StandardCharsets.UTF_8); if (inCall) { queue(new ByteBuffer[]{ByteBuffer.wrap(bytes)}, callback); return; } else { long responseContentLength = exchange.getResponseContentLength(); if(responseContentLength > 0 && bytes.length > responseContentLength) { callback.onException(exchange, this, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(bytes.length, responseContentLength)); return; } if (!exchange.isResponseStarted() && callback == IoCallback.END_EXCHANGE) { if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.setResponseContentLength(bytes.length); } } } try { outputStream.write(bytes); invokeOnComplete(callback); } catch (IOException e) { callback.onException(exchange, this, e); } }
@Override public void send(final ByteBuffer buffer) { send(buffer, IoCallback.END_EXCHANGE); }
@Override public void close(final IoCallback callback) { try { outputStream.close(); invokeOnComplete(callback); } catch (IOException e) { callback.onException(exchange, this, e); } }
private boolean writeBuffer(final ByteBuffer buffer, final IoCallback callback) { return writeBuffer(new ByteBuffer[]{buffer}, callback); }
@Override public void send(final String data, final Charset charset, final IoCallback callback) { byte[] bytes = data.getBytes(charset); if (inCall) { queue(new ByteBuffer[]{ByteBuffer.wrap(bytes)}, callback); return; }else { long responseContentLength = exchange.getResponseContentLength(); if(responseContentLength > 0 && bytes.length > responseContentLength) { callback.onException(exchange, this, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(bytes.length, responseContentLength)); return; } if (!exchange.isResponseStarted() && callback == IoCallback.END_EXCHANGE) { if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.setResponseContentLength(bytes.length); } } } try { outputStream.write(bytes); invokeOnComplete(callback); } catch (IOException e) { callback.onException(exchange, this, e); } }
@Override public void send(final String data, final Charset charset) { send(data, charset, IoCallback.END_EXCHANGE); }
private void invokeOnComplete(final IoCallback callback) { inCall = true; try { callback.onComplete(exchange, this); } finally { inCall = false; } while (next != null || pendingFile != null) { ByteBuffer[] next = this.next; IoCallback queuedCallback = this.queuedCallback; FileChannel file = this.pendingFile; this.next = null; this.queuedCallback = null; this.pendingFile = null; if (next != null) { for (ByteBuffer buffer : next) { writeBuffer(buffer, queuedCallback); } } else if (file != null) { performTransfer(file, queuedCallback); } inCall = true; try { queuedCallback.onComplete(exchange, this); } finally { inCall = false; } } }
@Override public void close(final IoCallback callback) { try { outputStream.close(); invokeOnComplete(callback); } catch (IOException e) { callback.onException(exchange, this, e); } }
private boolean writeBuffer(final ByteBuffer buffer, final IoCallback callback) { return writeBuffer(new ByteBuffer[]{buffer}, callback); }
@Override public void send(final ByteBuffer[] buffer, final IoCallback callback) { if (inCall) { queue(buffer, callback); return; } else { long responseContentLength = exchange.getResponseContentLength(); if(responseContentLength > 0 && Buffers.remaining(buffer) > responseContentLength) { callback.onException(exchange, this, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(Buffers.remaining(buffer), responseContentLength)); return; } if (!exchange.isResponseStarted() && callback == IoCallback.END_EXCHANGE) { if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.setResponseContentLength(Buffers.remaining(buffer)); } } } if (!writeBuffer(buffer, callback)) { return; } invokeOnComplete(callback); }
@Override public void transferFrom(FileChannel source, IoCallback callback) { if (inCall) { queue(source, callback); return; } performTransfer(source, callback); invokeOnComplete(callback); }
@Override public void send(final String data, final IoCallback callback) { byte[] bytes = data.getBytes(StandardCharsets.UTF_8); if (inCall) { queue(new ByteBuffer[]{ByteBuffer.wrap(bytes)}, callback); return; } else { long responseContentLength = exchange.getResponseContentLength(); if(responseContentLength > 0 && bytes.length > responseContentLength) { callback.onException(exchange, this, UndertowLogger.ROOT_LOGGER.dataLargerThanContentLength(bytes.length, responseContentLength)); return; } if (!exchange.isResponseStarted() && callback == IoCallback.END_EXCHANGE) { if (responseContentLength == -1 && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) { exchange.setResponseContentLength(bytes.length); } } } try { outputStream.write(bytes); invokeOnComplete(callback); } catch (IOException e) { callback.onException(exchange, this, e); } }
@Override public Sender getSender() { if (sender == null) { sender = new BlockingSenderImpl(exchange, getOutputStream()); } return sender; }
@Override public void send(final ByteBuffer[] buffer) { send(buffer, IoCallback.END_EXCHANGE); }
private void invokeOnComplete(final IoCallback callback) { inCall = true; try { callback.onComplete(exchange, this); } finally { inCall = false; } while (next != null || pendingFile != null) { ByteBuffer[] next = this.next; IoCallback queuedCallback = this.queuedCallback; FileChannel file = this.pendingFile; this.next = null; this.queuedCallback = null; this.pendingFile = null; if (next != null) { for (ByteBuffer buffer : next) { writeBuffer(buffer, queuedCallback); } } else if (file != null) { performTransfer(file, queuedCallback); } inCall = true; try { queuedCallback.onComplete(exchange, this); } finally { inCall = false; } } }
@Override public void close(final IoCallback callback) { try { outputStream.close(); invokeOnComplete(callback); } catch (IOException e) { callback.onException(exchange, this, e); } }