Skip to content

Commit 4b7cf85

Browse files
committed
Retrieve cached response body in a thread-safe manner
Closes gh-35745
1 parent a330277 commit 4b7cf85

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpResponseWrapper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
* into memory, thus allowing for multiple invocations of {@link #getBody()}.
3131
*
3232
* @author Arjen Poutsma
33+
* @author Juergen Hoeller
3334
* @since 3.1
3435
*/
3536
final class BufferingClientHttpResponseWrapper implements ClientHttpResponse {
3637

3738
private final ClientHttpResponse response;
3839

3940
@Nullable
40-
private byte[] body;
41+
private volatile byte[] body;
4142

4243

4344
BufferingClientHttpResponseWrapper(ClientHttpResponse response) {
@@ -62,10 +63,17 @@ public HttpHeaders getHeaders() {
6263

6364
@Override
6465
public InputStream getBody() throws IOException {
65-
if (this.body == null) {
66-
this.body = StreamUtils.copyToByteArray(this.response.getBody());
66+
byte[] body = this.body;
67+
if (body == null) {
68+
synchronized (this) {
69+
body = this.body;
70+
if (body == null) {
71+
body = StreamUtils.copyToByteArray(this.response.getBody());
72+
this.body = body;
73+
}
74+
}
6775
}
68-
return new ByteArrayInputStream(this.body);
76+
return new ByteArrayInputStream(body);
6977
}
7078

7179
@Override

0 commit comments

Comments
 (0)