@@ -246,30 +246,32 @@ default DataBuffer ensureCapacity(int capacity) {
246
246
default DataBuffer write (CharSequence charSequence , Charset charset ) {
247
247
Assert .notNull (charSequence , "CharSequence must not be null" );
248
248
Assert .notNull (charset , "Charset must not be null" );
249
- CharsetEncoder charsetEncoder = charset .newEncoder ()
250
- .onMalformedInput (CodingErrorAction .REPLACE )
251
- .onUnmappableCharacter (CodingErrorAction .REPLACE );
252
- CharBuffer inBuffer = CharBuffer .wrap (charSequence );
253
- int estimatedSize = (int ) (inBuffer .remaining () * charsetEncoder .averageBytesPerChar ());
254
- ByteBuffer outBuffer = ensureCapacity (estimatedSize )
255
- .asByteBuffer (writePosition (), writableByteCount ());
256
- while (true ) {
257
- CoderResult cr = (inBuffer .hasRemaining () ?
258
- charsetEncoder .encode (inBuffer , outBuffer , true ) : CoderResult .UNDERFLOW );
259
- if (cr .isUnderflow ()) {
260
- cr = charsetEncoder .flush (outBuffer );
261
- }
262
- if (cr .isUnderflow ()) {
263
- break ;
264
- }
265
- if (cr .isOverflow ()) {
266
- writePosition (outBuffer .position ());
267
- int maximumSize = (int ) (inBuffer .remaining () * charsetEncoder .maxBytesPerChar ());
268
- ensureCapacity (maximumSize );
269
- outBuffer = asByteBuffer (writePosition (), writableByteCount ());
249
+ if (charSequence .length () != 0 ) {
250
+ CharsetEncoder charsetEncoder = charset .newEncoder ()
251
+ .onMalformedInput (CodingErrorAction .REPLACE )
252
+ .onUnmappableCharacter (CodingErrorAction .REPLACE );
253
+ CharBuffer inBuffer = CharBuffer .wrap (charSequence );
254
+ int estimatedSize = (int ) (inBuffer .remaining () * charsetEncoder .averageBytesPerChar ());
255
+ ByteBuffer outBuffer = ensureCapacity (estimatedSize )
256
+ .asByteBuffer (writePosition (), writableByteCount ());
257
+ while (true ) {
258
+ CoderResult cr = (inBuffer .hasRemaining () ?
259
+ charsetEncoder .encode (inBuffer , outBuffer , true ) : CoderResult .UNDERFLOW );
260
+ if (cr .isUnderflow ()) {
261
+ cr = charsetEncoder .flush (outBuffer );
262
+ }
263
+ if (cr .isUnderflow ()) {
264
+ break ;
265
+ }
266
+ if (cr .isOverflow ()) {
267
+ writePosition (outBuffer .position ());
268
+ int maximumSize = (int ) (inBuffer .remaining () * charsetEncoder .maxBytesPerChar ());
269
+ ensureCapacity (maximumSize );
270
+ outBuffer = asByteBuffer (writePosition (), writableByteCount ());
271
+ }
270
272
}
273
+ writePosition (outBuffer .position ());
271
274
}
272
- writePosition (outBuffer .position ());
273
275
return this ;
274
276
}
275
277
0 commit comments