44
44
import org .springframework .core .codec .Hints ;
45
45
import org .springframework .core .io .buffer .DataBuffer ;
46
46
import org .springframework .core .io .buffer .DataBufferFactory ;
47
+ import org .springframework .core .io .buffer .DataBufferUtils ;
47
48
import org .springframework .core .log .LogFormatUtils ;
48
49
import org .springframework .http .MediaType ;
49
50
import org .springframework .http .codec .HttpMessageEncoder ;
@@ -161,11 +162,14 @@ private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBu
161
162
writer = customizeWriter (writer , mimeType , elementType , hints );
162
163
163
164
DataBuffer buffer = bufferFactory .allocateBuffer ();
165
+ boolean release = true ;
164
166
OutputStream outputStream = buffer .asOutputStream ();
165
167
166
168
try {
167
- JsonGenerator generator = getObjectMapper ().getFactory ().createGenerator (outputStream , encoding );
169
+ JsonGenerator generator =
170
+ getObjectMapper ().getFactory ().createGenerator (outputStream , encoding );
168
171
writer .writeValue (generator , value );
172
+ release = false ;
169
173
}
170
174
catch (InvalidDefinitionException ex ) {
171
175
throw new CodecException ("Type definition error: " + ex .getType (), ex );
@@ -174,7 +178,13 @@ private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBu
174
178
throw new EncodingException ("JSON encoding error: " + ex .getOriginalMessage (), ex );
175
179
}
176
180
catch (IOException ex ) {
177
- throw new IllegalStateException ("Unexpected I/O error while writing to data buffer" , ex );
181
+ throw new IllegalStateException ("Unexpected I/O error while writing to data buffer" ,
182
+ ex );
183
+ }
184
+ finally {
185
+ if (release ) {
186
+ DataBufferUtils .release (buffer );
187
+ }
178
188
}
179
189
180
190
return buffer ;
0 commit comments