示例#1
0
  /**
   * Asynchronous send of HTTP content.
   *
   * @param httpContent The HTTP content to send
   * @param callback The callback to use to notify success or failure
   */
  public void sendContent(HttpContent httpContent, Callback callback) {
    if (LOG.isDebugEnabled()) LOG.debug("sendContent(http={},{})", httpContent, callback);

    if (BufferUtil.hasContent(_aggregate)) {
      callback.failed(new IOException("cannot sendContent() after write()"));
      return;
    }
    if (_channel.isCommitted()) {
      callback.failed(new IOException("committed"));
      return;
    }

    while (true) {
      switch (_state.get()) {
        case OPEN:
          if (!_state.compareAndSet(OutputState.OPEN, OutputState.PENDING)) continue;
          break;

        case ERROR:
          callback.failed(new EofException(_onError));
          return;

        case CLOSED:
          callback.failed(new EofException("Closed"));
          return;

        default:
          throw new IllegalStateException();
      }
      break;
    }

    ByteBuffer buffer = _channel.useDirectBuffers() ? httpContent.getDirectBuffer() : null;
    if (buffer == null) buffer = httpContent.getIndirectBuffer();

    if (buffer != null) {
      sendContent(buffer, callback);
      return;
    }

    try {
      ReadableByteChannel rbc = httpContent.getReadableByteChannel();
      if (rbc != null) {
        // Close of the rbc is done by the async sendContent
        sendContent(rbc, callback);
        return;
      }

      InputStream in = httpContent.getInputStream();
      if (in != null) {
        sendContent(in, callback);
        return;
      }

      throw new IllegalArgumentException("unknown content for " + httpContent);
    } catch (Throwable th) {
      abort(th);
      callback.failed(th);
    }
  }
示例#2
0
  /**
   * Asynchronous send of content.
   *
   * @param httpContent The content to send
   * @param callback The callback to use to notify success or failure
   */
  public void sendContent(HttpContent httpContent, Callback callback) throws IOException {
    if (BufferUtil.hasContent(_aggregate)) throw new IOException("written");
    if (_channel.isCommitted()) throw new IOException("committed");

    while (true) {
      switch (_state.get()) {
        case OPEN:
          if (!_state.compareAndSet(State.OPEN, State.PENDING)) continue;
          break;
        case CLOSED:
          throw new EofException("Closed");
        default:
          throw new IllegalStateException();
      }
      break;
    }
    ByteBuffer buffer = _channel.useDirectBuffers() ? httpContent.getDirectBuffer() : null;
    if (buffer == null) buffer = httpContent.getIndirectBuffer();

    if (buffer != null) {
      sendContent(buffer, callback);
      return;
    }

    ReadableByteChannel rbc = httpContent.getReadableByteChannel();
    if (rbc != null) {
      // Close of the rbc is done by the async sendContent
      sendContent(rbc, callback);
      return;
    }

    InputStream in = httpContent.getInputStream();
    if (in != null) {
      sendContent(in, callback);
      return;
    }

    callback.failed(new IllegalArgumentException("unknown content for " + httpContent));
  }