use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project elasticsearch by elastic.
the class Netty4Utils method toByteBuf.
/**
* Turns the given BytesReference into a ByteBuf. Note: the returned ByteBuf will reference the internal
* pages of the BytesReference. Don't free the bytes of reference before the ByteBuf goes out of scope.
*/
public static ByteBuf toByteBuf(final BytesReference reference) {
if (reference.length() == 0) {
return Unpooled.EMPTY_BUFFER;
}
if (reference instanceof ByteBufBytesReference) {
return ((ByteBufBytesReference) reference).toByteBuf();
} else {
final BytesRefIterator iterator = reference.iterator();
// usually we have one, two, or three components from the header, the message, and a buffer
final List<ByteBuf> buffers = new ArrayList<>(3);
try {
BytesRef slice;
while ((slice = iterator.next()) != null) {
buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
}
final CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
composite.addComponents(true, buffers);
return composite;
} catch (IOException ex) {
throw new AssertionError("no IO happens here", ex);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project vert.x by eclipse.
the class VertxHandler method safeBuffer.
public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
if (buf == Unpooled.EMPTY_BUFFER) {
return buf;
}
if (buf.isDirect() || buf instanceof CompositeByteBuf) {
try {
if (buf.isReadable()) {
ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
buffer.writeBytes(buf);
return buffer;
} else {
return Unpooled.EMPTY_BUFFER;
}
} finally {
buf.release();
}
}
return buf;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project vert.x by eclipse.
the class HttpClientRequestImpl method write.
private void write(ByteBuf buff, boolean end) {
if (buff == null && !end) {
// nothing to write to the connection just return
return;
}
if (end) {
if (buff != null && !chunked && !contentLengthSet()) {
headers().set(CONTENT_LENGTH, String.valueOf(buff.writerIndex()));
}
} else {
if (!chunked && !contentLengthSet()) {
throw new IllegalStateException("You must set the Content-Length header to be the total size of the message " + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
}
}
if (buff != null) {
written += buff.readableBytes();
if (followRedirects > 0) {
if (cachedChunks == null) {
cachedChunks = Unpooled.compositeBuffer();
}
cachedChunks.addComponent(buff).writerIndex(cachedChunks.writerIndex() + buff.writerIndex());
}
}
if (stream == null) {
if (buff != null) {
if (pendingChunks == null) {
pendingChunks = buff;
} else {
CompositeByteBuf pending;
if (pendingChunks instanceof CompositeByteBuf) {
pending = (CompositeByteBuf) pendingChunks;
} else {
pending = Unpooled.compositeBuffer();
pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
pendingChunks = pending;
}
pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
}
}
connect(null);
} else {
if (!headWritten) {
writeHeadWithContent(buff, end);
} else {
stream.writeBuffer(buff, end);
}
if (end) {
stream.connection().reportBytesWritten(written);
if (respHandler != null) {
stream.endRequest();
}
}
}
if (end) {
completed = true;
if (completionHandler != null) {
completionHandler.handle(null);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class AbstractIntegrationTest method testIdentity.
protected void testIdentity(final byte[] data) {
final ByteBuf in = Unpooled.wrappedBuffer(data);
assertTrue(encoder.writeOutbound(in.retain()));
assertTrue(encoder.finish());
final CompositeByteBuf compressed = Unpooled.compositeBuffer();
ByteBuf msg;
while ((msg = encoder.readOutbound()) != null) {
compressed.addComponent(true, msg);
}
assertThat(compressed, is(notNullValue()));
decoder.writeInbound(compressed.retain());
assertFalse(compressed.isReadable());
final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
while ((msg = decoder.readInbound()) != null) {
decompressed.addComponent(true, msg);
}
assertEquals(in.resetReaderIndex(), decompressed);
compressed.release();
decompressed.release();
in.release();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project netty by netty.
the class FastLzIntegrationTest method testIdentity.
// test batched flow of data
@Override
protected void testIdentity(final byte[] data) {
final ByteBuf original = Unpooled.wrappedBuffer(data);
int written = 0, length = rand.nextInt(100);
while (written + length < data.length) {
ByteBuf in = Unpooled.wrappedBuffer(data, written, length);
encoder.writeOutbound(in);
written += length;
length = rand.nextInt(100);
}
ByteBuf in = Unpooled.wrappedBuffer(data, written, data.length - written);
encoder.writeOutbound(in);
encoder.finish();
ByteBuf msg;
final CompositeByteBuf compressed = Unpooled.compositeBuffer();
while ((msg = encoder.readOutbound()) != null) {
compressed.addComponent(true, msg);
}
assertThat(compressed, is(notNullValue()));
final byte[] compressedArray = new byte[compressed.readableBytes()];
compressed.readBytes(compressedArray);
written = 0;
length = rand.nextInt(100);
while (written + length < compressedArray.length) {
in = Unpooled.wrappedBuffer(compressedArray, written, length);
decoder.writeInbound(in);
written += length;
length = rand.nextInt(100);
}
in = Unpooled.wrappedBuffer(compressedArray, written, compressedArray.length - written);
decoder.writeInbound(in);
assertFalse(compressed.isReadable());
final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
while ((msg = decoder.readInbound()) != null) {
decompressed.addComponent(true, msg);
}
assertEquals(original, decompressed);
compressed.release();
decompressed.release();
original.release();
}
Aggregations