use of io.netty.buffer.CompositeByteBuf in project x-pipe by ctripcorp.
the class ByteBufUtils method readToBytes.
public static byte[] readToBytes(ByteBuf byteBuf) {
if (byteBuf instanceof CompositeByteBuf) {
CompositeByteBuf compositeByteBuf = (CompositeByteBuf) byteBuf;
ByteArrayOutputStream baous = new ByteArrayOutputStream();
for (ByteBuf single : compositeByteBuf) {
try {
baous.write(readToBytes(single));
} catch (IOException e) {
throw new IllegalStateException("write to ByteArrayOutputStream error", e);
}
}
return baous.toByteArray();
} else {
byte[] result = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(result);
return result;
}
}
use of io.netty.buffer.CompositeByteBuf in project x-pipe by ctripcorp.
the class SimpleTest method testNetty.
@Test
public void testNetty() {
CompositeByteBuf byteBuf = ByteBufAllocator.DEFAULT.compositeBuffer();
byteBuf.addComponent(Unpooled.wrappedBuffer("12345".getBytes()));
byteBuf.addComponent(Unpooled.wrappedBuffer("abcde".getBytes()));
System.out.println(ByteBufUtils.readToString(byteBuf));
ByteBuf buf = Unpooled.wrappedBuffer(Unpooled.wrappedBuffer("134".getBytes()), Unpooled.wrappedBuffer("abc".getBytes()));
System.out.println(buf.readableBytes());
byte[] result = new byte[buf.readableBytes()];
buf.readBytes(result);
System.out.println(new String(result));
}
use of io.netty.buffer.CompositeByteBuf in project incubator-servicecomb-java-chassis by apache.
the class TcpConnection method writeInContext.
protected void writeInContext() {
CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
for (; ; ) {
ByteBuf buf = writeQueue.poll();
if (buf == null) {
break;
}
writeQueueSize.decrementAndGet();
cbb.addComponent(true, buf);
if (cbb.numComponents() == cbb.maxNumComponents()) {
netSocket.write(Buffer.buffer(cbb));
cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
}
}
if (cbb.isReadable()) {
netSocket.write(Buffer.buffer(cbb));
}
}
use of io.netty.buffer.CompositeByteBuf in project jocean-http by isdom.
the class RxNettys method httpObjectsAsBytes.
public static byte[] httpObjectsAsBytes(final Iterator<HttpObject> itr) throws IOException {
final CompositeByteBuf composite = Unpooled.compositeBuffer();
try {
while (itr.hasNext()) {
final HttpObject obj = itr.next();
if (obj instanceof HttpContent) {
composite.addComponent(((HttpContent) obj).content());
}
}
composite.setIndex(0, composite.capacity());
@SuppressWarnings("resource") final InputStream is = new ByteBufInputStream(composite);
final byte[] bytes = new byte[is.available()];
is.read(bytes);
return bytes;
} finally {
ReferenceCountUtil.release(composite);
}
}
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();
}
}
});
}
Aggregations