Polishing and completing contribution
See gh-24866
This commit is contained in:
@@ -16,8 +16,11 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
@@ -26,17 +29,15 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Decoder for {@link ByteBuf ByteBufs}.
|
||||
*
|
||||
* @author Vladislav Kisel
|
||||
* @since 5.3
|
||||
*/
|
||||
public class ByteBufDecoder extends AbstractDataBufferDecoder<ByteBuf> {
|
||||
public class NettyByteBufDecoder extends AbstractDataBufferDecoder<ByteBuf> {
|
||||
|
||||
public ByteBufDecoder() {
|
||||
public NettyByteBufDecoder() {
|
||||
super(MimeTypeUtils.ALL);
|
||||
}
|
||||
|
||||
@@ -51,18 +52,17 @@ public class ByteBufDecoder extends AbstractDataBufferDecoder<ByteBuf> {
|
||||
public ByteBuf decode(DataBuffer dataBuffer, ResolvableType elementType,
|
||||
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
|
||||
|
||||
// Copies the dataBuffer if needed only
|
||||
ByteBuf byteBuf;
|
||||
if (dataBuffer instanceof NettyDataBuffer) {
|
||||
byteBuf = ((NettyDataBuffer) dataBuffer).getNativeBuffer();
|
||||
} else {
|
||||
byteBuf = Unpooled.wrappedBuffer(dataBuffer.asByteBuffer());
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(Hints.getLogPrefix(hints) + "Read " + byteBuf.readableBytes() + " bytes");
|
||||
logger.debug(Hints.getLogPrefix(hints) + "Read " + dataBuffer.readableByteCount() + " bytes");
|
||||
}
|
||||
if (dataBuffer instanceof NettyDataBuffer) {
|
||||
return ((NettyDataBuffer) dataBuffer).getNativeBuffer();
|
||||
}
|
||||
ByteBuf byteBuf;
|
||||
byte[] bytes = new byte[dataBuffer.readableByteCount()];
|
||||
dataBuffer.read(bytes);
|
||||
byteBuf = Unpooled.wrappedBuffer(bytes);
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
return byteBuf;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,12 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
@@ -25,9 +29,6 @@ import org.springframework.core.io.buffer.NettyDataBufferFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Encoder for {@link ByteBuf ByteBufs}.
|
||||
@@ -35,17 +36,17 @@ import java.util.Map;
|
||||
* @author Vladislav Kisel
|
||||
* @since 5.3
|
||||
*/
|
||||
public class ByteBufEncoder extends AbstractEncoder<ByteBuf> {
|
||||
public class NettyByteBufEncoder extends AbstractEncoder<ByteBuf> {
|
||||
|
||||
public ByteBufEncoder() {
|
||||
public NettyByteBufEncoder() {
|
||||
super(MimeTypeUtils.ALL);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
|
||||
Class<?> clazz = elementType.toClass();
|
||||
return super.canEncode(elementType, mimeType) && ByteBuf.class.isAssignableFrom(clazz);
|
||||
public boolean canEncode(ResolvableType type, @Nullable MimeType mimeType) {
|
||||
Class<?> clazz = type.toClass();
|
||||
return super.canEncode(type, mimeType) && ByteBuf.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,18 +62,16 @@ public class ByteBufEncoder extends AbstractEncoder<ByteBuf> {
|
||||
public DataBuffer encodeValue(ByteBuf byteBuf, DataBufferFactory bufferFactory,
|
||||
ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
|
||||
|
||||
DataBuffer dataBuffer;
|
||||
if (bufferFactory instanceof NettyDataBufferFactory) {
|
||||
dataBuffer = ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf);
|
||||
} else {
|
||||
dataBuffer = bufferFactory.wrap(byteBuf.nioBuffer());
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
|
||||
String logPrefix = Hints.getLogPrefix(hints);
|
||||
logger.debug(logPrefix + "Writing " + dataBuffer.readableByteCount() + " bytes");
|
||||
logger.debug(logPrefix + "Writing " + byteBuf.readableBytes() + " bytes");
|
||||
}
|
||||
return dataBuffer;
|
||||
if (bufferFactory instanceof NettyDataBufferFactory) {
|
||||
return ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf);
|
||||
}
|
||||
byte[] bytes = new byte[byteBuf.readableBytes()];
|
||||
byteBuf.readBytes(bytes);
|
||||
byteBuf.release();
|
||||
return bufferFactory.wrap(bytes);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,32 +16,33 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.testfixture.codec.AbstractDecoderTests;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Vladislav Kisel
|
||||
*/
|
||||
class ByteBufDecoderTests extends AbstractDecoderTests<ByteBufDecoder> {
|
||||
class NettyByteBufDecoderTests extends AbstractDecoderTests<NettyByteBufDecoder> {
|
||||
|
||||
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private final byte[] barBytes = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
|
||||
ByteBufDecoderTests() {
|
||||
super(new ByteBufDecoder());
|
||||
NettyByteBufDecoderTests() {
|
||||
super(new NettyByteBufDecoder());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -16,29 +16,30 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.testfixture.codec.AbstractEncoderTests;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Vladislav Kisel
|
||||
*/
|
||||
class ByteBufEncoderTests extends AbstractEncoderTests<ByteBufEncoder> {
|
||||
class NettyByteBufEncoderTests extends AbstractEncoderTests<NettyByteBufEncoder> {
|
||||
|
||||
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private final byte[] barBytes = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
ByteBufEncoderTests() {
|
||||
super(new ByteBufEncoder());
|
||||
NettyByteBufEncoderTests() {
|
||||
super(new NettyByteBufEncoder());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,7 +52,7 @@ class ByteBufEncoderTests extends AbstractEncoderTests<ByteBufEncoder> {
|
||||
assertThat(this.encoder.canEncode(ResolvableType.forClass(ByteBuf.class),
|
||||
MimeTypeUtils.APPLICATION_JSON)).isTrue();
|
||||
|
||||
// SPR-15464
|
||||
// gh-20024
|
||||
assertThat(this.encoder.canEncode(ResolvableType.NONE, null)).isFalse();
|
||||
}
|
||||
|
||||
@@ -60,12 +61,9 @@ class ByteBufEncoderTests extends AbstractEncoderTests<ByteBufEncoder> {
|
||||
public void encode() {
|
||||
Flux<ByteBuf> input = Flux.just(this.fooBytes, this.barBytes).map(Unpooled::copiedBuffer);
|
||||
|
||||
Unpooled.copiedBuffer(this.fooBytes, this.barBytes);
|
||||
|
||||
testEncodeAll(input, ByteBuf.class, step -> step
|
||||
.consumeNextWith(expectBytes(this.fooBytes))
|
||||
.consumeNextWith(expectBytes(this.barBytes))
|
||||
.verifyComplete());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user