List of usage examples for io.netty.buffer CompositeByteBuf CompositeByteBuf
public CompositeByteBuf(ByteBufAllocator alloc, boolean direct, int maxNumComponents, Iterable<ByteBuf> buffers)
From source file:io.grpc.alts.internal.AltsProtocolNegotiatorTest.java
License:Apache License
@Test @SuppressWarnings("unchecked") // List cast public void protectShouldRoundtrip() throws Exception { doHandshake();//from www .ja v a2 s . com // Write the message 1 character at a time. The message should be buffered // and not interfere with the handshake. final AtomicInteger writeCount = new AtomicInteger(); String message = "hello"; for (int ix = 0; ix < message.length(); ++ix) { ByteBuf in = Unpooled.copiedBuffer(message, ix, 1, UTF_8); @SuppressWarnings("unused") // go/futurereturn-lsc Future<?> possiblyIgnoredError = channel.write(in).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { writeCount.incrementAndGet(); } } }); } channel.flush(); // Capture the protected data written to the wire. assertEquals(1, channel.outboundMessages().size()); ByteBuf protectedData = channel.readOutbound(); assertEquals(message.length(), writeCount.get()); // Read the protected message at the server and verify it matches the original message. TsiFrameProtector serverProtector = serverHandshaker.createFrameProtector(channel.alloc()); List<ByteBuf> unprotected = new ArrayList<>(); serverProtector.unprotect(protectedData, (List<Object>) (List<?>) unprotected, channel.alloc()); // We try our best to remove the HTTP2 handler as soon as possible, but just by constructing it // a settings frame is written (and an HTTP2 preface). This is hard coded into Netty, so we // have to remove it here. See {@code Http2ConnectionHandler.PrefaceDecode.sendPreface}. int settingsFrameLength = 9; CompositeByteBuf unprotectedAll = new CompositeByteBuf(channel.alloc(), false, unprotected.size() + 1, unprotected); ByteBuf unprotectedData = unprotectedAll.slice(settingsFrameLength, message.length()); assertEquals(message, unprotectedData.toString(UTF_8)); // Protect the same message at the server. final AtomicReference<ByteBuf> newlyProtectedData = new AtomicReference<>(); serverProtector.protectFlush(Collections.singletonList(unprotectedData), new Consumer<ByteBuf>() { @Override public void accept(ByteBuf buf) { newlyProtectedData.set(buf); } }, channel.alloc()); // Read the protected message at the client and verify that it matches the original message. channel.writeInbound(newlyProtectedData.get()); assertEquals(1, channel.inboundMessages().size()); assertEquals(message, channel.<ByteBuf>readInbound().toString(UTF_8)); }
From source file:org.apache.arrow.flight.ArrowMessage.java
License:Apache License
/** * Convert the ArrowMessage to an InputStream. * @return InputStream//www . j a v a 2 s. co m */ private InputStream asInputStream(BufferAllocator allocator) { try { final ByteString bytes = ByteString.copyFrom(message.getByteBuffer(), message.getByteBuffer().remaining()); if (getMessageType() == HeaderType.SCHEMA) { final FlightData.Builder builder = FlightData.newBuilder().setDataHeader(bytes); if (descriptor != null) { builder.setFlightDescriptor(descriptor); } Preconditions.checkArgument(bufs.isEmpty()); return NO_BODY_MARSHALLER.stream(builder.build()); } Preconditions.checkArgument( getMessageType() == HeaderType.RECORD_BATCH || getMessageType() == HeaderType.DICTIONARY_BATCH); Preconditions.checkArgument(!bufs.isEmpty()); Preconditions.checkArgument(descriptor == null, "Descriptor should only be included in the schema message."); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CodedOutputStream cos = CodedOutputStream.newInstance(baos); cos.writeBytes(FlightData.DATA_HEADER_FIELD_NUMBER, bytes); if (appMetadata != null && appMetadata.capacity() > 0) { // Must call slice() as CodedOutputStream#writeByteBuffer writes -capacity- bytes, not -limit- bytes cos.writeByteBuffer(FlightData.APP_METADATA_FIELD_NUMBER, appMetadata.asNettyBuffer().nioBuffer().slice()); // This is weird, but implicitly, writing an ArrowMessage frees any references it has appMetadata.getReferenceManager().release(); } cos.writeTag(FlightData.DATA_BODY_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED); int size = 0; List<ByteBuf> allBufs = new ArrayList<>(); for (ArrowBuf b : bufs) { allBufs.add(b.asNettyBuffer()); size += b.readableBytes(); // [ARROW-4213] These buffers must be aligned to an 8-byte boundary in order to be readable from C++. if (b.readableBytes() % 8 != 0) { int paddingBytes = 8 - (b.readableBytes() % 8); assert paddingBytes > 0 && paddingBytes < 8; size += paddingBytes; allBufs.add(PADDING_BUFFERS.get(paddingBytes).retain()); } } // rawvarint is used for length definition. cos.writeUInt32NoTag(size); cos.flush(); ArrowBuf initialBuf = allocator.buffer(baos.size()); initialBuf.writeBytes(baos.toByteArray()); final CompositeByteBuf bb = new CompositeByteBuf(allocator.getAsByteBufAllocator(), true, bufs.size() + 1, ImmutableList.<ByteBuf>builder().add(initialBuf.asNettyBuffer()).addAll(allBufs).build()); // Implicitly, transfer ownership of our buffers to the input stream (which will decrement the refcount when done) final ByteBufInputStream is = new DrainableByteBufInputStream(bb); return is; } catch (Exception ex) { throw new RuntimeException("Unexpected IO Exception", ex); } }
From source file:org.elasticsearch.http.nio.PagedByteBuf.java
License:Apache License
static ByteBuf byteBufFromPages(InboundChannelBuffer.Page[] pages) { int componentCount = pages.length; if (componentCount == 0) { return Unpooled.EMPTY_BUFFER; } else if (componentCount == 1) { return byteBufFromPage(pages[0]); } else {//from w w w . ja v a 2 s . c om int maxComponents = Math.max(16, componentCount); final List<ByteBuf> components = new ArrayList<>(componentCount); for (InboundChannelBuffer.Page page : pages) { components.add(byteBufFromPage(page)); } return new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, maxComponents, components); } }
From source file:ratpack.rocker.internal.DefaultRockerRenderer.java
License:Apache License
@Override public void render(Context context, RockerModel rockerModel) throws Exception { try {//from w w w. j av a2s . c o m 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) { // https://github.com/fizzed/rocker/issues/30 // Ratpack will try to toString() the rockerModel object, to create an exception message for the RenderException // This will obscure the underlying exception. Log here so we can actually see it. // This can be removed when the above issue is rectified. throw new RendererException("Error rendering template " + rockerModel.getClass().getName(), e); } }