/** * Blocking send of content. * * @param content The content to send. * @throws IOException */ public void sendContent(ByteBuffer content) throws IOException { final BlockingCallback callback = _channel.getWriteBlockingCallback(); if (content.hasArray() && content.limit() < content.capacity()) content = content.asReadOnlyBuffer(); _channel.write(content, true, callback); callback.block(); }
@Override protected void blockForContent() throws IOException { while (true) { _httpConnection.fillInterested(_readBlocker); LOG.debug("{} block readable on {}", this, _readBlocker); _readBlocker.block(); Object content = getNextContent(); if (content != null || isFinished()) break; } }
@Override public void write(int b) throws IOException { _written += 1; boolean complete = _channel.getResponse().isAllContentWritten(_written); // Async or Blocking ? while (true) { switch (_state.get()) { case OPEN: if (_aggregate == null) _aggregate = _channel.getByteBufferPool().acquire(getBufferSize(), false); BufferUtil.append(_aggregate, (byte) b); // Check if all written or full if (complete || BufferUtil.isFull(_aggregate)) { BlockingCallback callback = _channel.getWriteBlockingCallback(); _channel.write(_aggregate, complete, callback); callback.block(); if (complete) closed(); } break; case ASYNC: throw new IllegalStateException("isReady() not called"); case READY: if (!_state.compareAndSet(State.READY, State.PENDING)) continue; if (_aggregate == null) _aggregate = _channel.getByteBufferPool().acquire(getBufferSize(), false); BufferUtil.append(_aggregate, (byte) b); // Check if all written or full if (!complete && !BufferUtil.isFull(_aggregate)) { if (!_state.compareAndSet(State.PENDING, State.ASYNC)) throw new IllegalStateException(); return; } // Do the asynchronous writing from the callback new AsyncFlush().process(); return; case PENDING: case UNREADY: throw new WritePendingException(); case CLOSED: throw new EofException("Closed"); } break; } }
/** * Blocking write, committing the response if needed. * * @param content the content buffer to write * @param complete whether the content is complete for the response * @throws IOException if the write fails */ protected void write(ByteBuffer content, boolean complete) throws IOException { sendResponse(null, content, complete, _writeblock); _writeblock.block(); }
protected boolean sendResponse(ResponseInfo info, ByteBuffer content, boolean complete) throws IOException { boolean committing = sendResponse(info, content, complete, _writeblock); _writeblock.block(); return committing; }
/** * Blocking send of content. * * @param content The content to send * @throws IOException */ public void sendContent(HttpContent content) throws IOException { final BlockingCallback callback = _channel.getWriteBlockingCallback(); sendContent(content, callback); callback.block(); }
/** * Blocking send of content. * * @param in The content to send * @throws IOException */ public void sendContent(ReadableByteChannel in) throws IOException { final BlockingCallback callback = _channel.getWriteBlockingCallback(); new ReadableByteChannelWritingCB(in, callback).iterate(); callback.block(); }
/** * Blocking send of content. * * @param in The content to send * @throws IOException */ public void sendContent(InputStream in) throws IOException { final BlockingCallback callback = _channel.getWriteBlockingCallback(); new InputStreamWritingCB(in, callback).iterate(); callback.block(); }