use of io.netty.buffer.CompositeByteBuf in project netty by netty.
the class DatagramUnicastTest method testSimpleSendCompositeDirectByteBuf.
public void testSimpleSendCompositeDirectByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
CompositeByteBuf buf = Unpooled.compositeBuffer();
buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
testSimpleSend0(sb, cb, buf, true, BYTES, 1);
CompositeByteBuf buf2 = Unpooled.compositeBuffer();
buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
testSimpleSend0(sb, cb, buf2, true, BYTES, 4);
}
use of io.netty.buffer.CompositeByteBuf in project ratpack by ratpack.
the class ByteBufComposingPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {
private Subscription subscription;
private CompositeByteBuf composite;
private volatile State state;
@Override
protected void onRequest(long n) {
if (subscription == null) {
upstream.subscribe(new Subscriber<ByteBuf>() {
@Override
public void onSubscribe(Subscription s) {
subscription = s;
state = State.Fetching;
s.request(1);
}
@Override
public void onNext(ByteBuf t) {
if (state == State.Closed) {
t.release();
return;
}
if (composite == null) {
composite = alloc.compositeBuffer(maxNum);
}
composite.addComponent(true, t);
if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
state = State.Writing;
emitNext(composite);
composite = null;
maybeFetch();
} else {
subscription.request(1);
}
}
@Override
public void onError(Throwable t) {
state = State.Closed;
ReferenceCountUtil.release(composite);
emitError(t);
}
@Override
public void onComplete() {
state = State.Closed;
if (composite != null) {
emitNext(composite);
}
emitComplete();
}
});
} else {
maybeFetch();
}
}
private void maybeFetch() {
if (getDemand() > 0 && state != State.Fetching) {
state = State.Fetching;
subscription.request(1);
}
}
@Override
protected void onCancel() {
state = State.Closed;
ReferenceCountUtil.release(composite);
if (subscription != null) {
subscription.cancel();
}
}
});
}
use of io.netty.buffer.CompositeByteBuf in project ratpack by ratpack.
the class ServerSentEventDecoder method str.
private String str(List<ByteBuf> bufs) {
if (bufs.isEmpty()) {
return null;
} else {
String str;
if (bufs.size() == 1) {
str = bufs.get(0).toString(StandardCharsets.UTF_8);
} else {
CompositeByteBuf composite = allocator.compositeBuffer(bufs.size() * 2 - 1);
Iterator<ByteBuf> iterator = bufs.iterator();
composite.addComponent(true, iterator.next());
while (iterator.hasNext()) {
composite.addComponent(true, NEWLINE_BYTEBUF.retainedDuplicate());
composite.addComponent(true, iterator.next());
}
str = composite.toString(StandardCharsets.UTF_8);
}
bufs.forEach(ByteBuf::release);
bufs.clear();
return str;
}
}
use of io.netty.buffer.CompositeByteBuf in project mongo-java-driver by mongodb.
the class NettyStream method readAsync.
@Override
public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
scheduleReadTimeout();
ByteBuf buffer = null;
Throwable exceptionResult = null;
synchronized (this) {
exceptionResult = pendingException;
if (exceptionResult == null) {
if (!hasBytesAvailable(numBytes)) {
pendingReader = new PendingReader(numBytes, handler);
} else {
CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
int bytesNeeded = numBytes;
for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter.hasNext(); ) {
io.netty.buffer.ByteBuf next = iter.next();
int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded);
if (bytesNeededFromCurrentBuffer == next.readableBytes()) {
composite.addComponent(next);
iter.remove();
} else {
next.retain();
composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer));
}
composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer);
bytesNeeded -= bytesNeededFromCurrentBuffer;
if (bytesNeeded == 0) {
break;
}
}
buffer = new NettyByteBuf(composite).flip();
}
}
}
if (exceptionResult != null) {
disableReadTimeout();
handler.failed(exceptionResult);
}
if (buffer != null) {
disableReadTimeout();
handler.completed(buffer);
}
}
use of io.netty.buffer.CompositeByteBuf in project spring-framework by spring-projects.
the class NettyDataBuffer method write.
/**
* Writes one or more Netty {@link ByteBuf}s to this buffer, starting at the current
* writing position.
* @param byteBufs the buffers to write into this buffer
* @return this buffer
*/
public NettyDataBuffer write(ByteBuf... byteBufs) {
Assert.notNull(byteBufs, "'byteBufs' must not be null");
CompositeByteBuf composite = new CompositeByteBuf(this.byteBuf.alloc(), this.byteBuf.isDirect(), byteBufs.length + 1);
composite.addComponent(this.byteBuf);
composite.addComponents(byteBufs);
int writerIndex = this.byteBuf.readableBytes() + Arrays.stream(byteBufs).mapToInt(ByteBuf::readableBytes).sum();
composite.writerIndex(writerIndex);
this.byteBuf = composite;
return this;
}
Aggregations