Пример #1
0
  private CompositeBuffer toCompositeInputContentBuffer() {
    if (!inputContentBuffer.isComposite()) {
      final CompositeBuffer compositeBuffer =
          CompositeBuffer.newBuffer(connection.getMemoryManager());

      compositeBuffer.allowBufferDispose(true);
      compositeBuffer.allowInternalBuffersDispose(true);

      int posAlign = 0;

      if (readAheadLimit > 0) { // the simple inputContentBuffer is marked
        // make the marked data still available
        inputContentBuffer.position(inputContentBuffer.position() - readCount);
        posAlign = readCount;

        markPos = 0; // for the CompositeBuffer markPos is 0
      }

      compositeBuffer.append(inputContentBuffer);
      compositeBuffer.position(posAlign);

      inputContentBuffer = compositeBuffer;
    }

    return (CompositeBuffer) inputContentBuffer;
  }
  private static Buffer appendContentChunkAndTrim(
      final MemoryManager memoryManager, final Buffer dstBuffer, final Buffer httpContentBuffer) {

    final boolean useDstBufferForHeaders =
        dstBuffer != null && dstBuffer.remaining() >= BODY_CHUNK_HEADER_SIZE;

    final Buffer headerBuffer;
    if (useDstBufferForHeaders) {
      headerBuffer = dstBuffer;
    } else {
      if (dstBuffer != null) {
        dstBuffer.trim();
      }
      headerBuffer = memoryManager.allocate(BODY_CHUNK_HEADER_SIZE);
      headerBuffer.allowBufferDispose(true);
    }

    headerBuffer.put((byte) 'A');
    headerBuffer.put((byte) 'B');
    headerBuffer.putShort((short) (4 + httpContentBuffer.remaining()));
    headerBuffer.put(AjpConstants.JK_AJP13_SEND_BODY_CHUNK);
    headerBuffer.putShort((short) httpContentBuffer.remaining());
    headerBuffer.trim();

    Buffer resultBuffer = Buffers.appendBuffers(memoryManager, headerBuffer, httpContentBuffer);

    // Add terminating \0
    final Buffer terminatingBuffer = memoryManager.allocate(1);
    terminatingBuffer.allowBufferDispose(true);

    resultBuffer = Buffers.appendBuffers(memoryManager, resultBuffer, terminatingBuffer);

    if (!useDstBufferForHeaders && dstBuffer != null) {
      resultBuffer = Buffers.appendBuffers(memoryManager, dstBuffer, resultBuffer);
    }

    if (resultBuffer.isComposite()) {
      // If during buffer appending - composite buffer was created -
      // allow buffer disposing
      ((CompositeBuffer) resultBuffer).allowInternalBuffersDispose(true);
    }

    return resultBuffer;
  }