Skip to content

Commit c3cf256

Browse files
committed
Merge remote-tracking branch 'origin/jetty-10.0.x' into jetty-11.0.x
2 parents cf97e58 + 3aaf39d commit c3cf256

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

jetty-util/src/main/java/org/eclipse/jetty/util/CharsetStringBuilder.java

+29-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Objects;
2424

2525
/**
26-
* <p>Build a string from a sequence of bytes.</p>
26+
* <p>Build a string from a sequence of bytes and/or characters.</p>
2727
* <p>Implementations of this interface are optimized for processing a mix of calls to already decoded
2828
* character based appends (e.g. {@link #append(char)} and calls to undecoded byte methods (e.g. {@link #append(byte)}.
2929
* This is particularly useful for decoding % encoded strings that are mostly already decoded but may contain
@@ -36,29 +36,51 @@
3636
*/
3737
public interface CharsetStringBuilder
3838
{
39+
/**
40+
* @param b An encoded byte to append
41+
*/
3942
void append(byte b);
4043

44+
/**
45+
* @param c A decoded character to append
46+
*/
4147
void append(char c);
4248

49+
/**
50+
* @param bytes Array of encoded bytes to append
51+
*/
4352
default void append(byte[] bytes)
4453
{
4554
append(bytes, 0, bytes.length);
4655
}
4756

57+
/**
58+
* @param b Array of encoded bytes
59+
* @param offset offset into the array
60+
* @param length the number of bytes to append from the array.
61+
*/
4862
default void append(byte[] b, int offset, int length)
4963
{
5064
int end = offset + length;
5165
for (int i = offset; i < end; i++)
5266
append(b[i]);
5367
}
5468

69+
/**
70+
* @param chars sequence of decoded characters
71+
* @param offset offset into the array
72+
* @param length the number of character to append from the sequence.
73+
*/
5574
default void append(CharSequence chars, int offset, int length)
5675
{
5776
int end = offset + length;
5877
for (int i = offset; i < end; i++)
5978
append(chars.charAt(i));
6079
}
6180

81+
/**
82+
* @param buf Buffer of encoded bytes to append. The bytes are consumed from the buffer.
83+
*/
6284
default void append(ByteBuffer buf)
6385
{
6486
int end = buf.position() + buf.remaining();
@@ -75,6 +97,10 @@ default void append(ByteBuffer buf)
7597

7698
void reset();
7799

100+
/**
101+
* @param charset The charset
102+
* @return A {@link CharsetStringBuilder} suitable for the charset.
103+
*/
78104
static CharsetStringBuilder forCharset(Charset charset)
79105
{
80106
Objects.requireNonNull(charset);
@@ -106,7 +132,7 @@ public void append(char c)
106132
@Override
107133
public void append(CharSequence chars, int offset, int length)
108134
{
109-
_builder.append(chars, offset, length);
135+
_builder.append(chars, offset, offset + length);
110136
}
111137

112138
@Override
@@ -145,7 +171,7 @@ public void append(char c)
145171
@Override
146172
public void append(CharSequence chars, int offset, int length)
147173
{
148-
_builder.append(chars, offset, length);
174+
_builder.append(chars, offset, offset + length);
149175
}
150176

151177
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//
2+
// ========================================================================
3+
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4+
//
5+
// This program and the accompanying materials are made available under the
6+
// terms of the Eclipse Public License v. 2.0 which is available at
7+
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
9+
//
10+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11+
// ========================================================================
12+
//
13+
14+
package org.eclipse.jetty.util;
15+
16+
import java.nio.ByteBuffer;
17+
import java.nio.charset.Charset;
18+
import java.nio.charset.StandardCharsets;
19+
import java.util.stream.Stream;
20+
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
import static org.hamcrest.Matchers.equalTo;
27+
import static org.hamcrest.Matchers.is;
28+
29+
// @checkstyle-disable-check : AvoidEscapedUnicodeCharactersCheck
30+
public class CharsetStringBuilderTest
31+
{
32+
public static Stream<Arguments> tests()
33+
{
34+
return Stream.of(
35+
Arguments.of("Hello World \uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-16 Æ\tÿ!!!", StandardCharsets.UTF_16),
36+
Arguments.of("Hello World \uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-8 Æ\tÿ!!!", StandardCharsets.UTF_8),
37+
Arguments.of("Now is the time for all good men to test US_ASCII \r\n\t!", StandardCharsets.US_ASCII),
38+
Arguments.of("How Now Brown Cow. Test iso 8859 Æ\tÿ!", StandardCharsets.ISO_8859_1)
39+
);
40+
}
41+
42+
@ParameterizedTest
43+
@MethodSource("tests")
44+
public void testBuilder(String test, Charset charset) throws Exception
45+
{
46+
byte[] bytes = test.getBytes(charset);
47+
48+
CharsetStringBuilder builder = CharsetStringBuilder.forCharset(charset);
49+
50+
builder.append(bytes);
51+
assertThat(builder.build(), equalTo(test));
52+
53+
for (byte b : bytes)
54+
builder.append(b);
55+
assertThat(builder.build(), equalTo(test));
56+
57+
builder.append(bytes[0]);
58+
builder.append(bytes, 1, bytes.length - 1);
59+
assertThat(builder.build(), equalTo(test));
60+
}
61+
62+
public static Stream<Charset> charsets()
63+
{
64+
return Stream.of(
65+
StandardCharsets.UTF_8,
66+
StandardCharsets.ISO_8859_1,
67+
StandardCharsets.US_ASCII,
68+
StandardCharsets.UTF_16
69+
);
70+
}
71+
72+
@ParameterizedTest
73+
@MethodSource("charsets")
74+
public void testBasicApi(Charset charset) throws Exception
75+
{
76+
CharsetStringBuilder builder = CharsetStringBuilder.forCharset(charset);
77+
ByteBuffer encoded = charset.encode("1");
78+
while (encoded.hasRemaining())
79+
builder.append(encoded.get());
80+
81+
builder.append('2');
82+
83+
builder.append(charset.encode("34"));
84+
85+
encoded = charset.encode("abc");
86+
int offset = encoded.remaining();
87+
encoded = charset.encode("abc56");
88+
int length = encoded.remaining() - offset;
89+
encoded = charset.encode("abc56xyz");
90+
byte[] bytes = new byte[1028];
91+
encoded.get(bytes, 0, encoded.remaining());
92+
builder.append(bytes, offset, length);
93+
94+
encoded = charset.encode("abc78xyz");
95+
encoded.position(offset);
96+
encoded.limit(offset + length);
97+
builder.append(encoded);
98+
99+
builder.append("9A", 0, 2);
100+
builder.append("xyzBCpqy", 3, 2);
101+
102+
assertThat(builder.build(), is("123456789ABC"));
103+
}
104+
}

0 commit comments

Comments
 (0)