diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/ByteArrayBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/ByteArrayBuilder.java index 6b78abcb2..f68797ac5 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/ByteArrayBuilder.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/ByteArrayBuilder.java @@ -74,8 +74,8 @@ public final class ByteArrayBuilder { final int capacity = (int) (in.remaining() * encoder.averageBytesPerChar()); ByteBuffer out = ensureFreeCapacity(buffer, capacity); - for (;;) { - CoderResult result = in.hasRemaining() ? encoder.encode(in, out, true) : CoderResult.UNDERFLOW; + while (in.hasRemaining()) { + CoderResult result = encoder.encode(in, out, true); if (result.isError()) { result.throwException(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java index 785afb21d..d43709fb9 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java @@ -96,6 +96,7 @@ public class TestByteArrayBuilder { buffer.append("bcd"); buffer.append("e"); buffer.append("f"); + buffer.append(""); buffer.append((String) null); buffer.append((char[]) null);