|
16 | 16 |
|
17 | 17 | package org.springframework.core.codec; |
18 | 18 |
|
19 | | -import java.nio.ByteBuffer; |
20 | | -import java.nio.CharBuffer; |
21 | 19 | import java.nio.charset.Charset; |
| 20 | +import java.nio.charset.CoderMalfunctionError; |
22 | 21 | import java.nio.charset.StandardCharsets; |
23 | 22 | import java.util.Map; |
24 | 23 |
|
|
28 | 27 | import org.springframework.core.ResolvableType; |
29 | 28 | import org.springframework.core.io.buffer.DataBuffer; |
30 | 29 | import org.springframework.core.io.buffer.DataBufferFactory; |
| 30 | +import org.springframework.core.io.buffer.DataBufferUtils; |
31 | 31 | import org.springframework.core.log.LogFormatUtils; |
32 | 32 | import org.springframework.lang.Nullable; |
33 | 33 | import org.springframework.util.MimeType; |
@@ -75,9 +75,21 @@ public Flux<DataBuffer> encode(Publisher<? extends CharSequence> inputStream, |
75 | 75 | return Hints.getLogPrefix(hints) + "Writing " + formatted; |
76 | 76 | }); |
77 | 77 | } |
78 | | - CharBuffer charBuffer = CharBuffer.wrap(charSequence); |
79 | | - ByteBuffer byteBuffer = charset.encode(charBuffer); |
80 | | - return bufferFactory.wrap(byteBuffer); |
| 78 | + boolean release = true; |
| 79 | + DataBuffer dataBuffer = bufferFactory.allocateBuffer(); |
| 80 | + try { |
| 81 | + dataBuffer.write(charSequence, charset); |
| 82 | + release = false; |
| 83 | + } |
| 84 | + catch (CoderMalfunctionError ex) { |
| 85 | + throw new EncodingException("String encoding error: " + ex.getMessage(), ex); |
| 86 | + } |
| 87 | + finally { |
| 88 | + if (release) { |
| 89 | + DataBufferUtils.release(dataBuffer); |
| 90 | + } |
| 91 | + } |
| 92 | + return dataBuffer; |
81 | 93 | }); |
82 | 94 | } |
83 | 95 |
|
|
0 commit comments