Read-Only HttpHeaders only created when necessary

This commit introduces a boolean `readOnly` flag in `HttpHeaders`,
which is used to check if creating a new `HttpHeaders` instance is
necessary in `readOnlyHttpHeaders`.
This commit is contained in:
Arjen Poutsma
2017-07-12 18:13:24 +02:00
parent 1764f905d8
commit 6329ccb327

View File

@@ -397,6 +397,8 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
private final Map<String, List<String>> headers;
private final boolean readOnly;
/**
* Constructs a new, empty instance of the {@code HttpHeaders} object.
@@ -422,6 +424,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
else {
this.headers = headers;
}
this.readOnly = readOnly;
}
@@ -1542,7 +1545,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* Return a {@code HttpHeaders} object that can only be read, not written to.
*/
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
return new HttpHeaders(headers, true);
return headers.readOnly ? headers : new HttpHeaders(headers, true);
}
}