PayloadUtils improvements and tests

This commit is contained in:
Rossen Stoyanchev
2019-07-30 16:12:10 +01:00
parent 22e87ac143
commit a1a8781279
3 changed files with 207 additions and 34 deletions

View File

@@ -24,14 +24,12 @@ import java.util.regex.Pattern;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.rsocket.metadata.CompositeMetadataFlyweight;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Encoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -165,18 +163,14 @@ final class MetadataEncoder {
try {
mergedMetadata.forEach((value, mimeType) -> {
DataBuffer buffer = encodeEntry(value, mimeType);
CompositeMetadataFlyweight.encodeAndAddMetadata(composite, this.allocator,
mimeType.toString(),
buffer instanceof NettyDataBuffer ?
((NettyDataBuffer) buffer).getNativeBuffer() :
Unpooled.wrappedBuffer(buffer.asByteBuffer()));
CompositeMetadataFlyweight.encodeAndAddMetadata(
composite, this.allocator, mimeType.toString(), PayloadUtils.asByteBuf(buffer));
});
if (bufferFactory() instanceof NettyDataBufferFactory) {
return ((NettyDataBufferFactory) bufferFactory()).wrap(composite);
}
else {
DataBuffer buffer = bufferFactory().allocateBuffer();
buffer.write(composite.nioBuffer());
DataBuffer buffer = bufferFactory().wrap(composite.nioBuffer());
composite.release();
return buffer;
}

View File

@@ -16,7 +16,10 @@
package org.springframework.messaging.rsocket;
import java.nio.ByteBuffer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.rsocket.Payload;
import io.rsocket.util.ByteBufPayload;
import io.rsocket.util.DefaultPayload;
@@ -64,41 +67,42 @@ public abstract class PayloadUtils {
/**
* Create a Payload from the given metadata and data.
* <p>If at least one is {@link NettyDataBuffer} then {@link ByteBufPayload}
* is created with either obtaining the underlying native {@link ByteBuf}
* or using {@link Unpooled#wrappedBuffer(ByteBuffer...)} if necessary.
* Otherwise, if both are {@link DefaultDataBuffer}, then
* {@link DefaultPayload} is created.
* @param metadata the metadata part for the payload
* @param data the data part for the payload
* @return the created 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());
}
return data instanceof NettyDataBuffer || metadata instanceof NettyDataBuffer ?
ByteBufPayload.create(asByteBuf(data), asByteBuf(metadata)) :
DefaultPayload.create(asByteBuffer(data), asByteBuffer(metadata));
}
/**
* Create a Payload from the given data.
* Create a Payload with data only. The created payload is
* {@link ByteBufPayload} if the input is {@link NettyDataBuffer} or
* otherwise it is {@link DefaultPayload}.
* @param data the data part for the payload
* @return the created Payload
* @return 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());
}
return data instanceof NettyDataBuffer ?
ByteBufPayload.create(asByteBuf(data)) : DefaultPayload.create(asByteBuffer(data));
}
static ByteBuf asByteBuf(DataBuffer buffer) {
return buffer instanceof NettyDataBuffer ?
((NettyDataBuffer) buffer).getNativeBuffer() : Unpooled.wrappedBuffer(buffer.asByteBuffer());
}
private static ByteBuffer asByteBuffer(DataBuffer buffer) {
return buffer instanceof DefaultDataBuffer ?
((DefaultDataBuffer) buffer).getNativeBuffer() : buffer.asByteBuffer();
}
}