public static Buffer appendContentAndTrim(
      final MemoryManager memoryManager, Buffer dstBuffer, Buffer httpContentBuffer) {
    Buffer resultBuffer = null;
    do {
      Buffer contentRemainder = null;
      if (httpContentBuffer.remaining() > MAX_BODY_CHUNK_CONTENT_SIZE) {
        contentRemainder =
            httpContentBuffer.split(httpContentBuffer.position() + MAX_BODY_CHUNK_CONTENT_SIZE);
      }

      final Buffer encodedContentChunk =
          appendContentChunkAndTrim(memoryManager, dstBuffer, httpContentBuffer);
      resultBuffer = Buffers.appendBuffers(memoryManager, resultBuffer, encodedContentChunk);

      // dstBuffer use only once, when it comes from caller
      dstBuffer = null;
      httpContentBuffer = contentRemainder;
    } while (httpContentBuffer != null && httpContentBuffer.hasRemaining());

    return resultBuffer;
  }
Пример #2
0
  /**
   * @param size the requested size of the {@link Buffer} to be returned.
   * @return the {@link Buffer} of a given size, which represents a chunk of the underlying {@link
   *     Buffer} which contains incoming request data. This method detaches the returned {@link
   *     Buffer}, so user code becomes responsible for handling its life-cycle.
   */
  public Buffer readBuffer(final int size) {
    if (LOGGER.isLoggable(LOGGER_LEVEL)) {
      log(
          "InputBuffer %s readBuffer(size), size: %s. Ready content: %s",
          this, size, inputContentBuffer);
    }

    final int remaining = inputContentBuffer.remaining();
    if (size > remaining) {
      throw new IllegalStateException("Can not read more bytes than available");
    }

    final Buffer buffer;
    if (size == remaining) {
      buffer = inputContentBuffer;
      inputContentBuffer = Buffers.EMPTY_BUFFER;
    } else {
      final Buffer tmpBuffer = inputContentBuffer.split(inputContentBuffer.position() + size);
      buffer = inputContentBuffer;
      inputContentBuffer = tmpBuffer;
    }

    return buffer;
  }