45
45
* @since 1.7.0
46
46
*/
47
47
public class GrizzlyResponse extends ResponseBase {
48
- private final Buffer responseBody ;
48
+
49
49
private final Boolean rfc6265Enabled ;
50
50
51
+ private Buffer responseBody ;
52
+ private boolean initialized ;
53
+
54
+
51
55
// ------------------------------------------------------------ Constructors
52
56
53
57
@@ -57,35 +61,19 @@ public GrizzlyResponse(final HttpResponseStatus status,
57
61
final boolean rfc6265Enabled ) {
58
62
super (status , headers , bodyParts );
59
63
this .rfc6265Enabled = rfc6265Enabled ;
60
- if (isNonEmpty (bodyParts )) {
61
- if (bodyParts .size () == 1 ) {
62
- responseBody = ((GrizzlyResponseBodyPart ) bodyParts .get (0 )).getBodyBuffer ();
63
- } else {
64
- final Buffer firstBuffer = ((GrizzlyResponseBodyPart ) bodyParts .get (0 )).getBodyBuffer ();
65
- final MemoryManager <?> mm = MemoryManager .DEFAULT_MEMORY_MANAGER ;
66
- Buffer constructedBodyBuffer = firstBuffer ;
67
- for (int i = 1 , len = bodyParts .size (); i < len ; i ++) {
68
- constructedBodyBuffer =
69
- Buffers .appendBuffers (mm ,
70
- constructedBodyBuffer ,
71
- ((GrizzlyResponseBodyPart ) bodyParts .get (i )).getBodyBuffer ());
72
- }
73
- responseBody = constructedBodyBuffer ;
74
- }
75
- } else {
76
- responseBody = Buffers .EMPTY_BUFFER ;
77
- }
78
64
}
79
65
80
66
67
+
68
+
81
69
// --------------------------------------------------- Methods from Response
82
70
83
71
/**
84
72
* {@inheritDoc}
85
73
*/
86
74
public InputStream getResponseBodyAsStream () throws IOException {
87
75
88
- return new BufferInputStream (responseBody );
76
+ return new BufferInputStream (getResponseBody0 () );
89
77
90
78
}
91
79
@@ -95,6 +83,7 @@ public InputStream getResponseBodyAsStream() throws IOException {
95
83
*/
96
84
public String getResponseBodyExcerpt (int maxLength , String charset ) throws IOException {
97
85
charset = calculateCharset (charset );
86
+ final Buffer responseBody = getResponseBody0 ();
98
87
final int len = Math .min (responseBody .remaining (), maxLength );
99
88
final int pos = responseBody .position ();
100
89
return responseBody .toStringContent (getCharset (charset ), pos , len + pos );
@@ -107,7 +96,7 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
107
96
*/
108
97
public String getResponseBody (String charset ) throws IOException {
109
98
110
- return responseBody .toStringContent (getCharset (charset ));
99
+ return getResponseBody0 () .toStringContent (getCharset (charset ));
111
100
112
101
}
113
102
@@ -149,7 +138,7 @@ public String getResponseBody() throws IOException {
149
138
*/
150
139
@ SuppressWarnings ("UnusedDeclaration" )
151
140
public Buffer getResponseBodyAsBuffer () {
152
- return responseBody ;
141
+ return getResponseBody0 () ;
153
142
}
154
143
155
144
/**
@@ -215,4 +204,34 @@ private Charset getCharset(final String charset) {
215
204
return Charsets .lookupCharset (charsetLocal );
216
205
217
206
}
207
+
208
+ private synchronized Buffer getResponseBody0 () {
209
+ if (!initialized ) {
210
+ if (isNonEmpty (bodyParts )) {
211
+ if (bodyParts .size () == 1 ) {
212
+ responseBody = ((GrizzlyResponseBodyPart ) bodyParts .get (
213
+ 0 )).getBodyBuffer ();
214
+ } else {
215
+ final Buffer firstBuffer =
216
+ ((GrizzlyResponseBodyPart ) bodyParts .get (
217
+ 0 )).getBodyBuffer ();
218
+ final MemoryManager <?> mm =
219
+ MemoryManager .DEFAULT_MEMORY_MANAGER ;
220
+ Buffer constructedBodyBuffer = firstBuffer ;
221
+ for (int i = 1 , len = bodyParts .size (); i < len ; i ++) {
222
+ constructedBodyBuffer =
223
+ Buffers .appendBuffers (mm ,
224
+ constructedBodyBuffer ,
225
+ ((GrizzlyResponseBodyPart ) bodyParts
226
+ .get (i )).getBodyBuffer ());
227
+ }
228
+ responseBody = constructedBodyBuffer ;
229
+ }
230
+ } else {
231
+ responseBody = Buffers .EMPTY_BUFFER ;
232
+ }
233
+ initialized = true ;
234
+ }
235
+ return responseBody ;
236
+ }
218
237
}
0 commit comments