diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/CharsetStringBuilder.java b/jetty-util/src/main/java/org/eclipse/jetty/util/CharsetStringBuilder.java index 341f39fda072..b31c70f41a98 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/CharsetStringBuilder.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/CharsetStringBuilder.java @@ -23,7 +23,7 @@ import java.util.Objects; /** - *
Build a string from a sequence of bytes.
+ *Build a string from a sequence of bytes and/or characters.
*Implementations of this interface are optimized for processing a mix of calls to already decoded
* character based appends (e.g. {@link #append(char)} and calls to undecoded byte methods (e.g. {@link #append(byte)}.
* This is particularly useful for decoding % encoded strings that are mostly already decoded but may contain
@@ -36,15 +36,29 @@
*/
public interface CharsetStringBuilder
{
+ /**
+ * @param b An encoded byte to append
+ */
void append(byte b);
+ /**
+ * @param c A decoded character to append
+ */
void append(char c);
+ /**
+ * @param bytes Array of encoded bytes to append
+ */
default void append(byte[] bytes)
{
append(bytes, 0, bytes.length);
}
+ /**
+ * @param b Array of encoded bytes
+ * @param offset offset into the array
+ * @param length the number of bytes to append from the array.
+ */
default void append(byte[] b, int offset, int length)
{
int end = offset + length;
@@ -52,6 +66,11 @@ default void append(byte[] b, int offset, int length)
append(b[i]);
}
+ /**
+ * @param chars sequence of decoded characters
+ * @param offset offset into the array
+ * @param length the number of character to append from the sequence.
+ */
default void append(CharSequence chars, int offset, int length)
{
int end = offset + length;
@@ -59,6 +78,9 @@ default void append(CharSequence chars, int offset, int length)
append(chars.charAt(i));
}
+ /**
+ * @param buf Buffer of encoded bytes to append. The bytes are consumed from the buffer.
+ */
default void append(ByteBuffer buf)
{
int end = buf.position() + buf.remaining();
@@ -75,6 +97,10 @@ default void append(ByteBuffer buf)
void reset();
+ /**
+ * @param charset The charset
+ * @return A {@link CharsetStringBuilder} suitable for the charset.
+ */
static CharsetStringBuilder forCharset(Charset charset)
{
Objects.requireNonNull(charset);
@@ -106,7 +132,7 @@ public void append(char c)
@Override
public void append(CharSequence chars, int offset, int length)
{
- _builder.append(chars, offset, length);
+ _builder.append(chars, offset, offset + length);
}
@Override
@@ -145,7 +171,7 @@ public void append(char c)
@Override
public void append(CharSequence chars, int offset, int length)
{
- _builder.append(chars, offset, length);
+ _builder.append(chars, offset, offset + length);
}
@Override
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/CharsetStringBuilderTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/CharsetStringBuilderTest.java
new file mode 100644
index 000000000000..246a9a009399
--- /dev/null
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/CharsetStringBuilderTest.java
@@ -0,0 +1,104 @@
+//
+// ========================================================================
+// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
+//
+// This program and the accompanying materials are made available under the
+// terms of the Eclipse Public License v. 2.0 which is available at
+// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
+// which is available at https://www.apache.org/licenses/LICENSE-2.0.
+//
+// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
+// ========================================================================
+//
+
+package org.eclipse.jetty.util;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+
+// @checkstyle-disable-check : AvoidEscapedUnicodeCharactersCheck
+public class CharsetStringBuilderTest
+{
+ public static Stream