Avoid repeated Charset resolution in MimeType

Closes gh-25808
This commit is contained in:
Juergen Hoeller
2020-09-25 10:51:08 +02:00
parent 214bc407b4
commit 9795883d91

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -103,6 +103,9 @@ public class MimeType implements Comparable<MimeType>, Serializable {
private final Map<String, String> parameters;
@Nullable
private Charset resolvedCharset;
@Nullable
private volatile String toStringValue;
@@ -138,6 +141,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
*/
public MimeType(String type, String subtype, Charset charset) {
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charset.name()));
this.resolvedCharset = charset;
}
/**
@@ -150,6 +154,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
*/
public MimeType(MimeType other, Charset charset) {
this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters()));
this.resolvedCharset = charset;
}
/**
@@ -194,11 +199,13 @@ public class MimeType implements Comparable<MimeType>, Serializable {
* Copy-constructor that copies the type, subtype and parameters of the given {@code MimeType},
* skipping checks performed in other constructors.
* @param other the other MimeType
* @since 5.3
*/
protected MimeType(MimeType other) {
this.type = other.type;
this.subtype = other.subtype;
this.parameters = other.parameters;
this.resolvedCharset = other.resolvedCharset;
this.toStringValue = other.toStringValue;
}
@@ -222,8 +229,9 @@ public class MimeType implements Comparable<MimeType>, Serializable {
Assert.hasLength(value, "'value' must not be empty");
checkToken(attribute);
if (PARAM_CHARSET.equals(attribute)) {
value = unquote(value);
Charset.forName(value);
if (this.resolvedCharset == null) {
this.resolvedCharset = Charset.forName(unquote(value));
}
}
else if (!isQuotedString(value)) {
checkToken(value);
@@ -304,8 +312,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
*/
@Nullable
public Charset getCharset() {
String charset = getParameter(PARAM_CHARSET);
return (charset != null ? Charset.forName(unquote(charset)) : null);
return this.resolvedCharset;
}
/**
@@ -393,17 +400,14 @@ public class MimeType implements Comparable<MimeType>, Serializable {
if (isWildcardSubtype() || other.isWildcardSubtype()) {
String thisSuffix = getSubtypeSuffix();
String otherSuffix = other.getSubtypeSuffix();
if (getSubtype().equals(WILDCARD_TYPE)
|| other.getSubtype().equals(WILDCARD_TYPE)) {
if (getSubtype().equals(WILDCARD_TYPE) || other.getSubtype().equals(WILDCARD_TYPE)) {
return true;
}
else if (isWildcardSubtype() && thisSuffix != null) {
return thisSuffix.equals(other.getSubtype())
|| thisSuffix.equals(otherSuffix);
return (thisSuffix.equals(other.getSubtype()) || thisSuffix.equals(otherSuffix));
}
else if (other.isWildcardSubtype() && otherSuffix != null) {
return this.getSubtype().equals(otherSuffix)
|| otherSuffix.equals(thisSuffix);
return (this.getSubtype().equals(otherSuffix) || otherSuffix.equals(thisSuffix));
}
}
}