Search in sources :

Example 56 with PooledByteBuffer

use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.

the class PipeliningBufferingStreamSinkConduit method write.

@Override
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
    if (anyAreSet(state, SHUTDOWN)) {
        throw new ClosedChannelException();
    }
    if (anyAreSet(state, FLUSHING)) {
        boolean res = flushBuffer();
        if (!res) {
            return 0;
        }
    }
    PooledByteBuffer pooled = this.buffer;
    if (pooled == null) {
        this.buffer = pooled = pool.allocate();
    }
    final ByteBuffer buffer = pooled.getBuffer();
    long total = Buffers.remaining(srcs, offset, length);
    if (buffer.remaining() > total) {
        long put = total;
        Buffers.copy(buffer, srcs, offset, length);
        return put;
    } else {
        return flushBufferWithUserData(srcs, offset, length);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 57 with PooledByteBuffer

use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.

the class AbstractFramedStreamSourceChannel method markStreamBroken.

/**
 * Called when this stream is no longer valid. Reads from the stream will result
 * in an exception.
 */
protected void markStreamBroken() {
    if (anyAreSet(state, STATE_STREAM_BROKEN)) {
        return;
    }
    synchronized (lock) {
        state |= STATE_STREAM_BROKEN;
        PooledByteBuffer data = this.data;
        if (data != null) {
            try {
                // may have been closed by the read thread
                data.close();
            } catch (Throwable e) {
            // ignore
            }
            this.data = null;
        }
        for (FrameData frame : pendingFrameData) {
            frame.frameData.close();
        }
        pendingFrameData.clear();
        if (isReadResumed()) {
            resumeReadsInternal(true);
        }
        if (waiters > 0) {
            lock.notifyAll();
        }
    }
}
Also used : PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 58 with PooledByteBuffer

use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.

the class HttpServerConnection method ungetRequestBytes.

/**
 * Pushes back the given data. This should only be used by transfer coding handlers that have read past
 * the end of the request when handling pipelined requests
 *
 * @param unget The buffer to push back
 */
public void ungetRequestBytes(final PooledByteBuffer unget) {
    if (getExtraBytes() == null) {
        setExtraBytes(unget);
    } else {
        PooledByteBuffer eb = getExtraBytes();
        ByteBuffer buf = eb.getBuffer();
        final ByteBuffer ugBuffer = unget.getBuffer();
        if (ugBuffer.limit() - ugBuffer.remaining() > buf.remaining()) {
            // stuff the existing data after the data we are ungetting
            ugBuffer.compact();
            ugBuffer.put(buf);
            ugBuffer.flip();
            eb.close();
            setExtraBytes(unget);
        } else {
            // TODO: this is horrible, but should not happen often
            final byte[] data = new byte[ugBuffer.remaining() + buf.remaining()];
            int first = ugBuffer.remaining();
            ugBuffer.get(data, 0, ugBuffer.remaining());
            buf.get(data, first, buf.remaining());
            eb.close();
            unget.close();
            final ByteBuffer newBuffer = ByteBuffer.wrap(data);
            setExtraBytes(new ImmediatePooledByteBuffer(newBuffer));
        }
    }
}
Also used : ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer)

Aggregations

PooledByteBuffer (io.undertow.connector.PooledByteBuffer)58 ByteBuffer (java.nio.ByteBuffer)47 IOException (java.io.IOException)29 ImmediatePooledByteBuffer (io.undertow.util.ImmediatePooledByteBuffer)10 StreamSourceChannel (org.xnio.channels.StreamSourceChannel)8 HttpServerExchange (io.undertow.server.HttpServerExchange)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ChannelListener (org.xnio.ChannelListener)5 HttpHandler (io.undertow.server.HttpHandler)4 HeaderMap (io.undertow.util.HeaderMap)4 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)4 SendFrameHeader (io.undertow.server.protocol.framed.SendFrameHeader)3 HttpString (io.undertow.util.HttpString)3 ByteBufferPool (io.undertow.connector.ByteBufferPool)2 HeaderValues (io.undertow.util.HeaderValues)2 InterruptedIOException (java.io.InterruptedIOException)2 CharBuffer (java.nio.CharBuffer)2 CharsetDecoder (java.nio.charset.CharsetDecoder)2 IoCallback (io.undertow.io.IoCallback)1