@@ -44,7 +44,8 @@ abstract class OnCommittedResponseWrapper extends HttpServletResponseWrapper {
4444 private long contentLength ;
4545
4646 /**
47- * The size of data written to the response body.
47+ * The size of data written to the response body. The field will only be updated when
48+ * {@link #disableOnCommitted} is false.
4849 */
4950 private long contentWritten ;
5051
@@ -58,10 +59,38 @@ abstract class OnCommittedResponseWrapper extends HttpServletResponseWrapper {
5859
5960 @ Override
6061 public void addHeader (String name , String value ) {
62+ checkContentLengthHeader (name , value );
63+ super .addHeader (name , value );
64+ }
65+
66+ @ Override
67+ public void addIntHeader (String name , int value ) {
68+ checkContentLengthHeader (name , value );
69+ super .addIntHeader (name , value );
70+ }
71+
72+ @ Override
73+ public void setHeader (String name , String value ) {
74+ checkContentLengthHeader (name , value );
75+ super .setHeader (name , value );
76+ }
77+
78+ @ Override
79+ public void setIntHeader (String name , int value ) {
80+ checkContentLengthHeader (name , value );
81+ super .setIntHeader (name , value );
82+ }
83+
84+ private void checkContentLengthHeader (String name , int value ) {
6185 if ("Content-Length" .equalsIgnoreCase (name )) {
86+ setContentLength (value );
87+ }
88+ }
89+
90+ private void checkContentLengthHeader (String name , String value ) {
91+ if (value != null && "Content-Length" .equalsIgnoreCase (name )) {
6292 setContentLength (Long .parseLong (value ));
6393 }
64- super .addHeader (name , value );
6594 }
6695
6796 @ Override
@@ -164,43 +193,64 @@ public void flushBuffer() throws IOException {
164193 }
165194
166195 private void trackContentLength (boolean content ) {
167- checkContentLength (content ? 4 : 5 ); // TODO Localization
196+ if (!this .disableOnCommitted ) {
197+ checkContentLength (content ? 4 : 5 ); // TODO Localization
198+ }
168199 }
169200
170201 private void trackContentLength (char content ) {
171- checkContentLength (1 );
202+ if (!this .disableOnCommitted ) {
203+ checkContentLength (1 );
204+ }
172205 }
173206
174207 private void trackContentLength (Object content ) {
175- trackContentLength (String .valueOf (content ));
208+ if (!this .disableOnCommitted ) {
209+ trackContentLength (String .valueOf (content ));
210+ }
176211 }
177212
178213 private void trackContentLength (byte [] content ) {
179- checkContentLength ((content != null ) ? content .length : 0 );
214+ if (!this .disableOnCommitted ) {
215+ checkContentLength ((content != null ) ? content .length : 0 );
216+ }
180217 }
181218
182219 private void trackContentLength (char [] content ) {
183- checkContentLength ((content != null ) ? content .length : 0 );
220+ if (!this .disableOnCommitted ) {
221+ checkContentLength ((content != null ) ? content .length : 0 );
222+ }
184223 }
185224
186225 private void trackContentLength (int content ) {
187- trackContentLength (String .valueOf (content ));
226+ if (!this .disableOnCommitted ) {
227+ trackContentLength (String .valueOf (content ));
228+ }
188229 }
189230
190231 private void trackContentLength (float content ) {
191- trackContentLength (String .valueOf (content ));
232+ if (!this .disableOnCommitted ) {
233+ trackContentLength (String .valueOf (content ));
234+ }
192235 }
193236
194237 private void trackContentLength (double content ) {
195- trackContentLength (String .valueOf (content ));
238+ if (!this .disableOnCommitted ) {
239+ trackContentLength (String .valueOf (content ));
240+ }
196241 }
197242
198243 private void trackContentLengthLn () {
199- trackContentLength ("\r \n " );
244+ if (!this .disableOnCommitted ) {
245+ trackContentLength ("\r \n " );
246+ }
200247 }
201248
202249 private void trackContentLength (String content ) {
203- checkContentLength (content .length ());
250+ if (!this .disableOnCommitted ) {
251+ int contentLength = (content != null ) ? content .length () : 4 ;
252+ checkContentLength (contentLength );
253+ }
204254 }
205255
206256 /**
@@ -453,7 +503,7 @@ public PrintWriter format(Locale l, String format, Object... args) {
453503
454504 @ Override
455505 public PrintWriter append (CharSequence csq ) {
456- checkContentLength (csq .length ());
506+ checkContentLength (( csq != null ) ? csq .length () : 4 );
457507 return this .delegate .append (csq );
458508 }
459509
0 commit comments