Reactor StringEncoder into CharSequenceEncoder
This commit refactors the StringEncoder to a CharSequenceEncoder, in order to support StringBuilders, Groovy GStrings, etc. Issue: https://github.com/spring-projects/spring-reactive/issues/120
This commit is contained in:
committed by
Rossen Stoyanchev
parent
0f6505e32f
commit
b0d7625e3e
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@@ -28,18 +30,19 @@ import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.util.MimeType;
|
||||
|
||||
/**
|
||||
* Encode from a String stream to a bytes stream.
|
||||
* Encode from a CharSequence stream to a bytes stream.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
* @see StringDecoder
|
||||
*/
|
||||
public class StringEncoder extends AbstractEncoder<String> {
|
||||
public class CharSequenceEncoder extends AbstractEncoder<CharSequence> {
|
||||
|
||||
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
|
||||
public StringEncoder() {
|
||||
public CharSequenceEncoder() {
|
||||
super(new MimeType("text", "plain", DEFAULT_CHARSET));
|
||||
}
|
||||
|
||||
@@ -47,11 +50,11 @@ public class StringEncoder extends AbstractEncoder<String> {
|
||||
@Override
|
||||
public boolean canEncode(ResolvableType elementType, MimeType mimeType, Object... hints) {
|
||||
Class<?> clazz = elementType.getRawClass();
|
||||
return (super.canEncode(elementType, mimeType, hints) && String.class.equals(clazz));
|
||||
return (super.canEncode(elementType, mimeType, hints) && CharSequence.class.isAssignableFrom(clazz));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<DataBuffer> encode(Publisher<? extends String> inputStream,
|
||||
public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream,
|
||||
DataBufferFactory bufferFactory, ResolvableType elementType,
|
||||
MimeType mimeType, Object... hints) {
|
||||
|
||||
@@ -62,11 +65,10 @@ public class StringEncoder extends AbstractEncoder<String> {
|
||||
else {
|
||||
charset = DEFAULT_CHARSET;
|
||||
}
|
||||
return Flux.from(inputStream).map(s -> {
|
||||
byte[] bytes = s.getBytes(charset);
|
||||
DataBuffer dataBuffer = bufferFactory.allocateBuffer(bytes.length);
|
||||
dataBuffer.write(bytes);
|
||||
return dataBuffer;
|
||||
return Flux.from(inputStream).map(charSequence -> {
|
||||
CharBuffer charBuffer = CharBuffer.wrap(charSequence);
|
||||
ByteBuffer byteBuffer = charset.encode(charBuffer);
|
||||
return bufferFactory.wrap(byteBuffer);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ import org.springframework.util.MimeTypeUtils;
|
||||
* @author Arjen Poutsma
|
||||
* @author Mark Paluch
|
||||
* @since 5.0
|
||||
* @see StringEncoder
|
||||
* @see CharSequenceEncoder
|
||||
*/
|
||||
public class StringDecoder extends AbstractDecoder<String> {
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import reactor.test.TestSubscriber;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.support.DataBufferUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
@@ -37,35 +38,46 @@ import static org.junit.Assert.assertTrue;
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StringEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class CharSequenceEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private StringEncoder encoder;
|
||||
private CharSequenceEncoder encoder;
|
||||
|
||||
@Before
|
||||
public void createEncoder() {
|
||||
this.encoder = new StringEncoder();
|
||||
this.encoder = new CharSequenceEncoder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canWrite() {
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(StringBuilder.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(StringBuffer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class), MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(String.class), MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws InterruptedException {
|
||||
Flux<String> output = Flux.from(
|
||||
this.encoder.encode(Flux.just("foo"), this.bufferFactory, null, null))
|
||||
.map(chunk -> {
|
||||
byte[] b = new byte[chunk.readableByteCount()];
|
||||
chunk.read(b);
|
||||
DataBufferUtils.release(chunk);
|
||||
return new String(b, StandardCharsets.UTF_8);
|
||||
});
|
||||
public void writeString() throws InterruptedException {
|
||||
Flux<String> stringFlux = Flux.just("foo");
|
||||
Flux<DataBuffer> output = Flux.from(
|
||||
this.encoder.encode(stringFlux, this.bufferFactory, null, null));
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertValues("foo");
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(stringConsumer("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeStringBuilder() throws InterruptedException {
|
||||
Flux<StringBuilder> stringBuilderFlux = Flux.just(new StringBuilder("foo"));
|
||||
Flux<DataBuffer> output = Flux.from(
|
||||
this.encoder.encode(stringBuilderFlux, this.bufferFactory, null, null));
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(stringConsumer("foo"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user