提交 92e1bff7 编写于 作者: A Arjen Poutsma

Merge pull request #84 from mp911de/charsetdecoder-in-stringdecoder

Use CharsetDecoder to decode a DataBuffer into a String.
......@@ -16,6 +16,7 @@
package org.springframework.core.codec.support;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
......@@ -39,6 +40,7 @@ import org.springframework.util.MimeTypeUtils;
* @author Sebastien Deleuze
* @author Brian Clozel
* @author Arjen Poutsma
* @author Mark Paluch
* @see StringEncoder
*/
public class StringDecoder extends AbstractDecoder<String> {
......@@ -83,9 +85,8 @@ public class StringDecoder extends AbstractDecoder<String> {
}
Charset charset = getCharset(mimeType);
return inputFlux.map(content -> {
byte[] bytes = new byte[content.readableByteCount()];
content.read(bytes);
return new String(bytes, charset);
CharBuffer charBuffer = charset.decode(content.asByteBuffer());
return charBuffer.toString();
});
}
......
......@@ -33,6 +33,7 @@ import static org.junit.Assert.*;
/**
* @author Sebastien Deleuze
* @author Brian Clozel
* @author Mark Paluch
*/
public class StringDecoderTests extends AbstractAllocatingTestCase {
......@@ -94,4 +95,13 @@ public class StringDecoderTests extends AbstractAllocatingTestCase {
assertEquals("foobar", result);
}
@Test
public void decodeEmpty() throws InterruptedException {
Mono<DataBuffer> source = Mono.just(stringBuffer(""));
Flux<String> output =
this.decoder.decode(source, ResolvableType.forClass(String.class), null);
TestSubscriber<String> testSubscriber = new TestSubscriber<>();
testSubscriber.bindTo(output).assertValues("");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册