Search in sources :

Example 1 with ControlledByteBuffer

use of org.apache.synapse.transport.passthru.util.ControlledByteBuffer in project wso2-synapse by wso2.

the class Pipe method copyAndConsume.

/**
 * Consume the data from the buffer. Same as {@link Pipe#consume(org.apache.http.nio.ContentEncoder)} but this gives
 * a copy of data which has been consumed.
 *
 * @param encoder encoder used to write
 * @return a buffer with data written (consumed)
 * @throws IOException if an error occurred while consuming data
 */
public ByteBuffer copyAndConsume(final ContentEncoder encoder) throws IOException {
    if (consumerIoControl == null) {
        throw new IllegalStateException("Consumer cannot be null when calling consume");
    }
    if (hasHttpProducer && producerIoControl == null) {
        throw new IllegalStateException("Producer cannot be null when calling consume");
    }
    lock.lock();
    ControlledByteBuffer consumerBuffer = getConsumerBuffer();
    try {
        // if producer at error we have to stop the encoding and return immediately
        if (producerError) {
            encoder.complete();
            return null;
        }
        setOutputMode(consumerBuffer);
        // clone original buffer
        ByteBuffer originalBuffer = consumerBuffer.getByteBuffer();
        int bytesWritten = encoder.write(originalBuffer);
        ByteBuffer duplicate = originalBuffer.duplicate();
        // replicate positions of original buffer in duplicated buffer
        int position = originalBuffer.position();
        duplicate.limit(position);
        if (bytesWritten > 0) {
            duplicate.position(position - bytesWritten);
        }
        consumePostActions(consumerBuffer, encoder, bytesWritten);
        return duplicate;
    } finally {
        lock.unlock();
    }
}
Also used : ControlledByteBuffer(org.apache.synapse.transport.passthru.util.ControlledByteBuffer) ControlledByteBuffer(org.apache.synapse.transport.passthru.util.ControlledByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ControlledByteBuffer

use of org.apache.synapse.transport.passthru.util.ControlledByteBuffer in project wso2-synapse by wso2.

the class Pipe method consume.

/**
 * Consume the data from the buffer. Before calling this method attachConsumer
 * method must be called with a valid IOControl.
 *
 * @param encoder encoder used to write the data means there will not be any data
 *                written in to this buffer
 * @return number of bytes written (consumed)
 * @throws IOException if an error occurred while consuming data
 */
public int consume(final ContentEncoder encoder) throws IOException {
    if (consumerIoControl == null) {
        throw new IllegalStateException("Consumer cannot be null when calling consume");
    }
    if (hasHttpProducer && producerIoControl == null) {
        throw new IllegalStateException("Producer cannot be null when calling consume");
    }
    lock.lock();
    ControlledByteBuffer consumerBuffer = getConsumerBuffer();
    try {
        // if producer at error we have to stop the encoding and return immediately
        if (producerError) {
            encoder.complete();
            return -1;
        }
        setOutputMode(consumerBuffer);
        int bytesWritten = encoder.write(consumerBuffer.getByteBuffer());
        consumePostActions(consumerBuffer, encoder, bytesWritten);
        return bytesWritten;
    } finally {
        lock.unlock();
    }
}
Also used : ControlledByteBuffer(org.apache.synapse.transport.passthru.util.ControlledByteBuffer)

Example 3 with ControlledByteBuffer

use of org.apache.synapse.transport.passthru.util.ControlledByteBuffer in project wso2-synapse by wso2.

the class SourceContext method reset.

/**
 * Reset the resources associated with this context
 *
 * @param isError whether an error is causing this shutdown of the connection.
 *                It is very important to set this flag correctly.
 *                When an error causing the shutdown of the connections we should not
 *                release associated writer buffer to the pool as it might lead into
 *                situations like same buffer is getting released to both source and target
 *                buffer factories
 */
public void reset(boolean isError) {
    this.request = null;
    this.response = null;
    this.state = ProtocolState.REQUEST_READY;
    if (writer != null) {
        if (!isError) {
            // If there is an error we do not release the buffer to the factory
            ControlledByteBuffer buffer = writer.getBuffer();
            sourceConfiguration.getBufferFactory().release(buffer);
        }
    }
    this.reader = null;
    this.writer = null;
}
Also used : ControlledByteBuffer(org.apache.synapse.transport.passthru.util.ControlledByteBuffer)

Example 4 with ControlledByteBuffer

use of org.apache.synapse.transport.passthru.util.ControlledByteBuffer in project wso2-synapse by wso2.

the class TargetContext method reset.

/**
 * Reset the resources associated with this context
 *
 * @param isError whether an error is causing this shutdown of the connection.
 *                It is very important to set this flag correctly.
 *                When an error causing the shutdown of the connections we should not
 *                release associated writer buffer to the pool as it might lead into
 *                situations like same buffer is getting released to both source and target
 *                buffer factories
 */
public void reset(boolean isError) {
    request = null;
    response = null;
    if (writer != null) {
        if (!isError) {
            // If there is an error we do not release the buffer to the factory
            ControlledByteBuffer buffer = writer.getBuffer();
            targetConfiguration.getBufferFactory().release(buffer);
        }
    }
    reader = null;
    writer = null;
}
Also used : ControlledByteBuffer(org.apache.synapse.transport.passthru.util.ControlledByteBuffer)

Aggregations

ControlledByteBuffer (org.apache.synapse.transport.passthru.util.ControlledByteBuffer)4 ByteBuffer (java.nio.ByteBuffer)1