Skip to content

Commit

Permalink
Polishing DataBuffer::write(CharSequence, Charset)
Browse files Browse the repository at this point in the history
  • Loading branch information
poutsma committed Feb 13, 2023
1 parent 026be04 commit 79a1fcb
Showing 1 changed file with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import java.io.Closeable;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
Expand Down Expand Up @@ -265,32 +263,31 @@ default DataBuffer ensureCapacity(int capacity) {
default DataBuffer write(CharSequence charSequence, Charset charset) {
Assert.notNull(charSequence, "CharSequence must not be null");
Assert.notNull(charset, "Charset must not be null");
if (charSequence.length() != 0) {
CharsetEncoder charsetEncoder = charset.newEncoder()
if (charSequence.length() > 0) {
CharsetEncoder encoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer src = CharBuffer.wrap(charSequence);
int length = (int) (src.remaining() * charsetEncoder.maxBytesPerChar());
ensureWritable(length);
try (ByteBufferIterator iterator = writableByteBuffers()) {
Assert.state(iterator.hasNext(), "No ByteBuffer available");
ByteBuffer dest = iterator.next();
int pos = dest.position();
CoderResult cr = charsetEncoder.encode(src, dest, true);
if (!cr.isUnderflow()) {
cr.throwException();
int cap = (int) (src.remaining() * encoder.averageBytesPerChar());
while (true) {
ensureWritable(cap);
CoderResult cr;
try (ByteBufferIterator iterator = writableByteBuffers()) {
Assert.state(iterator.hasNext(), "No ByteBuffer available");
ByteBuffer dest = iterator.next();
cr = encoder.encode(src, dest, true);
if (cr.isUnderflow()) {
cr = encoder.flush(dest);
}
writePosition(dest.position());
}
cr = charsetEncoder.flush(dest);
if (!cr.isUnderflow()) {
cr.throwException();
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
cap = 2 * cap + 1;
}
length = dest.position() - pos;
}
catch (CharacterCodingException ex) {
// should not happen, because the encoder uses action REPLACE
throw new UncheckedIOException(ex);
}
writePosition(writePosition() + length);
}
return this;
}
Expand Down

0 comments on commit 79a1fcb

Please sign in to comment.