1 /*
2 * Copyright 2008-2019 by Emeric Vernat
3 *
4 * This file is part of Java Melody.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package net.bull.javamelody.internal.web;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22
23 import javax.servlet.http.HttpServletResponse;
24
25 /**
26 * Implémentation de ServletOutputStream qui fonctionne avec le {@link CounterServletResponseWrapper}.
27 * @author Emeric Vernat
28 */
29 public class CounterResponseStream extends FilterServletOutputStream {
30 private long dataLength;
31
32 /**
33 * Construit un servlet output stream associé avec la réponse spécifiée.
34 * @param response HttpServletResponse
35 * @throws IOException Erreur d'entrée/sortie
36 */
37 CounterResponseStream(HttpServletResponse response) throws IOException {
38 super(response.getOutputStream());
39 }
40
41 /**
42 * Construit un servlet output stream associé avec l'output stream spécifiée.
43 * @param output OutputStream
44 */
45 public CounterResponseStream(OutputStream output) {
46 super(output);
47 }
48
49 /**
50 * Retourne la valeur de la propriété dataLength.
51 * @return long
52 */
53 public long getDataLength() {
54 return dataLength;
55 }
56
57 /**
58 * Réinitialiser dataLength à 0.
59 */
60 public void reset() {
61 dataLength = 0;
62 }
63
64 /** {@inheritDoc} */
65 @Override
66 public void write(int i) throws IOException {
67 super.write(i);
68 dataLength++;
69 }
70
71 /** {@inheritDoc} */
72 @Override
73 public void write(byte[] bytes) throws IOException {
74 super.write(bytes);
75 dataLength += bytes.length;
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public void write(byte[] bytes, int off, int len) throws IOException {
81 super.write(bytes, off, len);
82 dataLength += len;
83 }
84 }
85