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.ServletOutputStream;
24 import javax.servlet.WriteListener;
25
26 /**
27  * Output stream pour un filtre implémentant {@link ServletOutputStream} à partir d'un autre ServletOutputStream ou d'un OutputStream.
28  * @author Emeric Vernat
29  */

30 public class FilterServletOutputStream extends ServletOutputStream {
31     private final OutputStream stream;
32     private final ServletOutputStream servletOutputStream;
33     private boolean closed;
34
35     /**
36      * Constructeur.
37      * @param output ServletOutputStream
38      */

39     FilterServletOutputStream(ServletOutputStream output) {
40         super();
41         assert output != null;
42         stream = output;
43         servletOutputStream = output;
44     }
45
46     /**
47      * Constructeur.
48      * @param output OutputStream
49      */

50     public FilterServletOutputStream(OutputStream output) {
51         super();
52         assert output != null;
53         stream = output;
54         servletOutputStream = null;
55     }
56
57     /** {@inheritDoc} */
58     @Override
59     public void close() throws IOException {
60         stream.close();
61         closed = true;
62     }
63
64     /** {@inheritDoc} */
65     @Override
66     public void flush() throws IOException {
67         // issue 532: do not flush a closed output stream
68         if (!closed) {
69             stream.flush();
70         }
71     }
72
73     /** {@inheritDoc} */
74     @Override
75     public void write(int b) throws IOException {
76         stream.write(b);
77     }
78
79     /** {@inheritDoc} */
80     @Override
81     public void write(byte[] bytes) throws IOException {
82         stream.write(bytes);
83     }
84
85     /** {@inheritDoc} */
86     @Override
87     public void write(byte[] bytes, int off, int len) throws IOException {
88         stream.write(bytes, off, len);
89     }
90
91     @Override
92     public boolean isReady() {
93         if (servletOutputStream != null) {
94             return servletOutputStream.isReady();
95         }
96         return true;
97     }
98
99     @Override
100     public void setWriteListener(WriteListener writeListener) {
101         if (servletOutputStream != null) {
102             servletOutputStream.setWriteListener(writeListener);
103         }
104     }
105 }
106