Avoid ByteArrayOutputStream for source values without the need to be encoded

Closes gh-24154
This commit is contained in:
Juergen Hoeller
2019-12-09 13:56:50 +01:00
parent 197dbffe20
commit 5dbd3b0bbf

View File

@@ -335,8 +335,21 @@ final class HierarchicalUriComponents extends UriComponents {
Assert.notNull(type, "Type must not be null");
byte[] bytes = source.getBytes(charset);
boolean original = true;
for (byte b : bytes) {
if (b < 0) {
b += 256;
}
if (!type.isAllowed(b)) {
original = false;
break;
}
}
if (original) {
return source;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
boolean changed = false;
for (byte b : bytes) {
if (b < 0) {
b += 256;
@@ -350,10 +363,9 @@ final class HierarchicalUriComponents extends UriComponents {
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
bos.write(hex1);
bos.write(hex2);
changed = true;
}
}
return (changed ? new String(bos.toByteArray(), charset) : source);
return new String(bos.toByteArray(), charset);
}
private Type getHostType() {
@@ -416,7 +428,6 @@ final class HierarchicalUriComponents extends UriComponents {
@Override
protected HierarchicalUriComponents expandInternal(UriTemplateVariables uriVariables) {
Assert.state(!this.encodeState.equals(EncodeState.FULLY_ENCODED),
"URI components already encoded, and could not possibly contain '{' or '}'.");