use of io.rsocket.Payload in project spring-framework by spring-projects.
the class PayloadUtilsTests method createWithNettyBuffers.
@Test
public void createWithNettyBuffers() {
NettyDataBuffer data = createNettyDataBuffer("sample data");
NettyDataBuffer metadata = createNettyDataBuffer("sample metadata");
Payload payload = PayloadUtils.createPayload(data, metadata);
try {
assertThat(payload).isInstanceOf(ByteBufPayload.class);
assertThat(payload.data()).isSameAs(data.getNativeBuffer());
assertThat(payload.metadata()).isSameAs(metadata.getNativeBuffer());
} finally {
payload.release();
}
}
use of io.rsocket.Payload in project spring-framework by spring-projects.
the class PayloadUtilsTests method createWithNettyBuffer.
@Test
public void createWithNettyBuffer() {
NettyDataBuffer data = createNettyDataBuffer("sample data");
Payload payload = PayloadUtils.createPayload(data);
try {
assertThat(payload).isInstanceOf(ByteBufPayload.class);
assertThat(payload.data()).isSameAs(data.getNativeBuffer());
} finally {
payload.release();
}
}
use of io.rsocket.Payload in project spring-framework by spring-projects.
the class PayloadUtilsTests method createWithDefaultBuffer.
@Test
public void createWithDefaultBuffer() {
DataBuffer data = createDefaultDataBuffer("sample data");
Payload payload = PayloadUtils.createPayload(data);
assertThat(payload).isInstanceOf(DefaultPayload.class);
assertThat(payload.getDataUtf8()).isEqualTo(data.toString(UTF_8));
}
use of io.rsocket.Payload in project spring-framework by spring-projects.
the class DefaultRSocketRequesterBuilder method getSetupPayload.
private Mono<Payload> getSetupPayload(MimeType dataMimeType, MimeType metaMimeType, RSocketStrategies strategies) {
Object data = this.setupData;
boolean hasMetadata = (this.setupRoute != null || !CollectionUtils.isEmpty(this.setupMetadata));
if (!hasMetadata && data == null) {
return Mono.just(EMPTY_SETUP_PAYLOAD);
}
Mono<DataBuffer> dataMono = Mono.empty();
if (data != null) {
ReactiveAdapter adapter = strategies.reactiveAdapterRegistry().getAdapter(data.getClass());
Assert.isTrue(adapter == null || !adapter.isMultiValue(), "Expected single value: " + data);
Mono<?> mono = (adapter != null ? Mono.from(adapter.toPublisher(data)) : Mono.just(data));
dataMono = mono.map(value -> {
ResolvableType type = ResolvableType.forClass(value.getClass());
Encoder<Object> encoder = strategies.encoder(type, dataMimeType);
Assert.notNull(encoder, () -> "No encoder for " + dataMimeType + ", " + type);
return encoder.encodeValue(value, strategies.dataBufferFactory(), type, dataMimeType, HINTS);
});
}
Mono<DataBuffer> metaMono = Mono.empty();
if (hasMetadata) {
metaMono = new MetadataEncoder(metaMimeType, strategies).metadataAndOrRoute(this.setupMetadata, this.setupRoute, this.setupRouteVars).encode();
}
Mono<DataBuffer> emptyBuffer = Mono.fromCallable(() -> strategies.dataBufferFactory().wrap(EMPTY_BYTE_ARRAY));
dataMono = dataMono.switchIfEmpty(emptyBuffer);
metaMono = metaMono.switchIfEmpty(emptyBuffer);
return Mono.zip(dataMono, metaMono).map(tuple -> PayloadUtils.createPayload(tuple.getT1(), tuple.getT2())).doOnDiscard(DataBuffer.class, DataBufferUtils::release).doOnDiscard(Payload.class, Payload::release);
}
use of io.rsocket.Payload in project spring-framework by spring-projects.
the class PayloadUtilsTests method retainAndReleaseWithNettyFactory.
@Test
public void retainAndReleaseWithNettyFactory() {
Payload payload = ByteBufPayload.create("sample data");
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, this.nettyBufferFactory);
try {
assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isEqualTo(1);
assertThat(payload.refCnt()).isEqualTo(0);
} finally {
DataBufferUtils.release(buffer);
}
}
Aggregations