org.springframework.core.io.buffer.NettyDataBuffer Java Examples

The following examples show how to use org.springframework.core.io.buffer.NettyDataBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingNettyDataBufferFactory_HttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT));

    final HttpData httpData1 = HttpData.ofUtf8("abc");

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
    assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne();

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).content())
            .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer());
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
}
 
Example #2
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingNettyDataBufferFactory_PooledHttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT));

    final PooledHttpData httpData1 =
            PooledHttpData.wrap(Unpooled.wrappedBuffer("abc".getBytes()));

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
    assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne();

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).content())
            .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer());
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
}
 
Example #3
Source File: PayloadUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a Payload from the given metadata and data.
 * @param metadata the metadata part for the payload
 * @param data the data part for the payload
 * @return the created Payload
 */
public static Payload createPayload(DataBuffer metadata, DataBuffer data) {
	if (metadata instanceof NettyDataBuffer && data instanceof NettyDataBuffer) {
		return ByteBufPayload.create(
				((NettyDataBuffer) data).getNativeBuffer(),
				((NettyDataBuffer) metadata).getNativeBuffer());
	}
	else if (metadata instanceof DefaultDataBuffer && data instanceof DefaultDataBuffer) {
		return DefaultPayload.create(
				((DefaultDataBuffer) data).getNativeBuffer(),
				((DefaultDataBuffer) metadata).getNativeBuffer());
	}
	else {
		return DefaultPayload.create(data.asByteBuffer(), metadata.asByteBuffer());
	}
}
 
Example #4
Source File: BodyExtractorsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test // SPR-17054
public void unsupportedMediaTypeShouldConsumeAndCancel() {
	NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
	NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
	TestPublisher<DataBuffer> body = TestPublisher.create();

	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
	response.setBody(body.flux());

	BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
	StepVerifier.create(extractor.extract(response, this.context))
			.then(() -> {
				body.assertWasSubscribed();
				body.emit(buffer);
			})
			.expectErrorSatisfies(throwable -> {
				assertTrue(throwable instanceof UnsupportedMediaTypeException);
				try {
					buffer.release();
					Assert.fail("releasing the buffer should have failed");
				} catch (IllegalReferenceCountException exc) {

				}
				body.assertCancelled();
			}).verify();
}
 
Example #5
Source File: ByteBufLeakTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void ensureAllBuffersAreReleased() {
    await().untilAsserted(() -> {
        NettyDataBuffer buffer;
        while ((buffer = allocatedBuffers.peek()) != null) {
            assertThat(buffer.getNativeBuffer().refCnt()).isZero();
            allocatedBuffers.poll();
        }
        assertThat(allocatedBuffers).isEmpty();
    });
}
 
Example #6
Source File: ByteBufLeakTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Bean
public DataBufferFactory dataBufferFactory() {
    return new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT) {
        // This method will be called when emitting string from Mono/Flux.
        @Override
        public NettyDataBuffer allocateBuffer(int initialCapacity) {
            final NettyDataBuffer buffer = super.allocateBuffer(initialCapacity);
            // Keep allocated buffers.
            allocatedBuffers.offer(buffer);
            return buffer;
        }
    };
}
 
Example #7
Source File: DataBufferFactoryWrapper.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link DataBuffer} into an {@link HttpData}.
 */
HttpData toHttpData(DataBuffer dataBuffer) {
    if (dataBuffer instanceof NettyDataBuffer) {
        return PooledHttpData.wrap((((NettyDataBuffer) dataBuffer).getNativeBuffer()));
    }
    final ByteBuffer buf =
            dataBuffer instanceof DefaultDataBuffer ? ((DefaultDataBuffer) dataBuffer).getNativeBuffer()
                                                    : dataBuffer.asByteBuffer();
    return PooledHttpData.wrap(Unpooled.wrappedBuffer(buf));
}
 
Example #8
Source File: ServerWebExchangeUtils.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private static ServerHttpRequest decorate(ServerWebExchange exchange,
		DataBuffer dataBuffer, boolean cacheDecoratedRequest) {
	if (dataBuffer.readableByteCount() > 0) {
		if (log.isTraceEnabled()) {
			log.trace("retaining body in exchange attribute");
		}
		exchange.getAttributes().put(CACHED_REQUEST_BODY_ATTR, dataBuffer);
	}

	ServerHttpRequest decorator = new ServerHttpRequestDecorator(
			exchange.getRequest()) {
		@Override
		public Flux<DataBuffer> getBody() {
			return Mono.<DataBuffer>fromSupplier(() -> {
				if (exchange.getAttributeOrDefault(CACHED_REQUEST_BODY_ATTR,
						null) == null) {
					// probably == downstream closed or no body
					return null;
				}
				// TODO: deal with Netty
				NettyDataBuffer pdb = (NettyDataBuffer) dataBuffer;
				return pdb.factory().wrap(pdb.getNativeBuffer().retainedSlice());
			}).flux();
		}
	};
	if (cacheDecoratedRequest) {
		exchange.getAttributes().put(CACHED_SERVER_HTTP_REQUEST_DECORATOR_ATTR,
				decorator);
	}
	return decorator;
}
 
Example #9
Source File: NettyClientResponsePlugin.java    From soul with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
    return Mono.defer(() -> {
        Connection connection = exchange.getAttribute(Constants.CLIENT_RESPONSE_CONN_ATTR);
        if (connection == null) {
            return Mono.empty();
        }
        if (log.isTraceEnabled()) {
            log.trace("NettyWriteResponseFilter start inbound: "
                    + connection.channel().id().asShortText() + ", outbound: "
                    + exchange.getLogPrefix());
        }
        ServerHttpResponse response = exchange.getResponse();
        NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory();
        final Flux<NettyDataBuffer> body = connection
                .inbound()
                .receive()
                .retain()
                .map(factory::wrap);
        MediaType contentType = response.getHeaders().getContentType();
        return isStreamingMediaType(contentType)
                ? response.writeAndFlushWith(body.map(Flux::just))
                : response.writeWith(body);

    })
            .then(chain.execute(exchange)
                    .doOnError(throwable -> cleanup(exchange))).doOnCancel(() -> cleanup(exchange));
}
 
Example #10
Source File: ByteBufDecoder.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf decode(DataBuffer dataBuffer, ResolvableType elementType,
                      @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    if (dataBuffer instanceof NettyDataBuffer) {
        return ((NettyDataBuffer) dataBuffer).getNativeBuffer();
    }
    return Unpooled.wrappedBuffer(dataBuffer.asByteBuffer());
}
 
Example #11
Source File: ByteBufDecoder.java    From alibaba-rsocket-broker with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf decode(DataBuffer dataBuffer, ResolvableType elementType,
                      @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    if (dataBuffer instanceof NettyDataBuffer) {
        return ((NettyDataBuffer) dataBuffer).getNativeBuffer();
    }
    return Unpooled.wrappedBuffer(dataBuffer.asByteBuffer());
}
 
Example #12
Source File: RSocketBufferLeakTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private DataBuffer recordHint(DataBuffer buffer) {
	AssertionError error = new AssertionError(String.format(
			"DataBuffer leak: {%s} {%s} not released.%nStacktrace at buffer creation: ", buffer,
			ObjectUtils.getIdentityHexString(((NettyDataBuffer) buffer).getNativeBuffer())));
	this.created.add(new DataBufferLeakInfo(buffer, error));
	return buffer;
}
 
Example #13
Source File: RSocketBufferLeakTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public NettyDataBuffer wrap(ByteBuf byteBuf) {
	NettyDataBuffer dataBuffer = super.wrap(byteBuf);
	if (byteBuf != Unpooled.EMPTY_BUFFER) {
		recordHint(dataBuffer);
	}
	return dataBuffer;
}
 
Example #14
Source File: PayloadUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a Payload from the given data.
 * @param data the data part for the payload
 * @return the created Payload
 */
public static Payload createPayload(DataBuffer data) {
	if (data instanceof NettyDataBuffer) {
		return ByteBufPayload.create(((NettyDataBuffer) data).getNativeBuffer());
	}
	else if (data instanceof DefaultDataBuffer) {
		return DefaultPayload.create(((DefaultDataBuffer) data).getNativeBuffer());
	}
	else {
		return DefaultPayload.create(data.asByteBuffer());
	}
}
 
Example #15
Source File: TagsMetadata.java    From spring-cloud-rsocket with Apache License 2.0 4 votes vote down vote up
public static ByteBuf asByteBuf(DataBuffer buffer) {
	return buffer instanceof NettyDataBuffer
			? ((NettyDataBuffer) buffer).getNativeBuffer()
			: Unpooled.wrappedBuffer(buffer.asByteBuffer());
}
 
Example #16
Source File: MetadataEncoder.java    From spring-cloud-rsocket with Apache License 2.0 4 votes vote down vote up
static ByteBuf asByteBuf(DataBuffer buffer) {
	return buffer instanceof NettyDataBuffer
			? ((NettyDataBuffer) buffer).getNativeBuffer()
			: Unpooled.wrappedBuffer(buffer.asByteBuffer());
}
 
Example #17
Source File: BodyExtractorsTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test // SPR-17054
public void unsupportedMediaTypeShouldConsumeAndCancel() {
	NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
	NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
	TestPublisher<DataBuffer> body = TestPublisher.create();

	MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
	response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
	response.setBody(body.flux());

	BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
	StepVerifier.create(extractor.extract(response, this.context))
			.then(() -> {
				body.assertWasSubscribed();
				body.emit(buffer);
			})
			.expectErrorSatisfies(throwable -> {
				assertTrue(throwable instanceof UnsupportedMediaTypeException);
				try {
					buffer.release();
					Assert.fail("releasing the buffer should have failed");
				}
				catch (IllegalReferenceCountException exc) {
				}
				body.assertCancelled();
			}).verify();
}
 
Example #18
Source File: RSocketBufferLeakTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public NettyDataBuffer allocateBuffer(int initialCapacity) {
	return (NettyDataBuffer) recordHint(super.allocateBuffer(initialCapacity));
}
 
Example #19
Source File: RSocketBufferLeakTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public NettyDataBuffer allocateBuffer() {
	return (NettyDataBuffer) recordHint(super.allocateBuffer());
}
 
Example #20
Source File: ArmeriaServerHttpResponseTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void requestInvalidDemand() throws Exception {
    final ConcurrentLinkedQueue<NettyDataBuffer> allocatedBuffers = new ConcurrentLinkedQueue<>();
    final DataBufferFactoryWrapper<NettyDataBufferFactory> factoryWrapper = new DataBufferFactoryWrapper<>(
            new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT) {
                @Override
                public NettyDataBuffer allocateBuffer() {
                    final NettyDataBuffer buffer = super.allocateBuffer();
                    allocatedBuffers.offer(buffer);
                    return buffer;
                }
            });
    final CompletableFuture<HttpResponse> future = new CompletableFuture<>();
    final ArmeriaServerHttpResponse response =
            new ArmeriaServerHttpResponse(ctx, future, factoryWrapper, null);
    response.writeWith(Mono.just(factoryWrapper.delegate().allocateBuffer().write("foo".getBytes())))
            .then(Mono.defer(response::setComplete)).subscribe();
    await().until(future::isDone);
    assertThat(future.isCompletedExceptionally()).isFalse();

    final AtomicBoolean completed = new AtomicBoolean();
    final AtomicReference<Throwable> error = new AtomicReference<>();
    future.get().subscribe(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(0);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            // Do nothing.
        }

        @Override
        public void onError(Throwable t) {
            error.compareAndSet(null, t);
            completed.set(true);
        }

        @Override
        public void onComplete() {
            completed.set(true);
        }
    });

    await().untilTrue(completed);
    assertThat(error.get()).isInstanceOf(IllegalArgumentException.class)
                           .hasMessageContaining("Reactive Streams specification rule 3.9");
    await().untilAsserted(() -> {
        assertThat(allocatedBuffers).hasSize(1);
        assertThat(allocatedBuffers.peek().getNativeBuffer().refCnt()).isZero();
        allocatedBuffers.poll();
    });
}
 
Example #21
Source File: MessagingRSocket.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private int refCount(DataBuffer dataBuffer) {
	return dataBuffer instanceof NettyDataBuffer ?
			((NettyDataBuffer) dataBuffer).getNativeBuffer().refCnt() : 1;
}