Пример #1
0
 /**
  * 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();
 }
Пример #2
0
  @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;
    }
  }
Пример #3
0
  @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;
    }
  }
Пример #4
0
 /**
  * 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();
 }
Пример #5
0
 protected boolean sendResponse(ResponseInfo info, ByteBuffer content, boolean complete)
     throws IOException {
   boolean committing = sendResponse(info, content, complete, _writeblock);
   _writeblock.block();
   return committing;
 }
Пример #6
0
 /**
  * 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();
 }
Пример #7
0
 /**
  * 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();
 }
Пример #8
0
 /**
  * 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();
 }