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);
}
}
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();
}
}
}
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));
}
}
}
Aggregations