use of org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project ratpack by ratpack.
the class DefaultRockerRenderer method render.
@Override
public void render(Context context, RockerModel rockerModel) throws Exception {
try {
ArrayOfByteArraysOutput output = rockerModel.render(ArrayOfByteArraysOutput.FACTORY);
List<byte[]> arrays = output.getArrays();
ByteBuf byteBuf;
int size = arrays.size();
if (size == 0) {
byteBuf = Unpooled.EMPTY_BUFFER;
} else if (size == 1) {
byteBuf = Unpooled.wrappedBuffer(arrays.get(0));
} else {
byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, size, Iterables.transform(arrays, Unpooled::wrappedBuffer));
}
AsciiString contentType = output.getContentType() == ContentType.HTML ? HTML : TEXT;
context.getResponse().contentTypeIfNotSet(contentType).send(byteBuf);
} catch (Exception e) {
// This can be removed when the above issue is rectified.
throw new RendererException("Error rendering template " + rockerModel.getClass().getName(), e);
}
}
Aggregations