Avoid repeated Charset resolution in MimeType
Closes gh-25808
This commit is contained in:
parent
bf00db3c6c
commit
c04400890f
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -197,7 +202,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
|
|||
* @see <a href="https://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
|
||||
*/
|
||||
private void checkToken(String token) {
|
||||
for (int i = 0; i < token.length(); i++ ) {
|
||||
for (int i = 0; i < token.length(); i++) {
|
||||
char ch = token.charAt(i);
|
||||
if (!TOKEN.get(ch)) {
|
||||
throw new IllegalArgumentException("Invalid token character '" + ch + "' in token \"" + token + "\"");
|
||||
|
|
@ -210,8 +215,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);
|
||||
|
|
@ -279,8 +285,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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue