View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.view;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import javax.servlet.ServletOutputStream;
25  
26  /**
27   * Delegates the standard OutputStream-methods (close, flush, write)
28   * to the given ServletOutputStream, if the ResponseSwitch is enabled
29   *  
30   * @author Werner Punz (latest modification by $Author: lu4242 $)
31   * @author Jakob Korherr 
32   * @version $Revision: 1151677 $ $Date: 2011-07-27 19:03:59 -0500 (Wed, 27 Jul 2011) $
33   */
34  class SwitchableOutputStream extends ServletOutputStream
35  {
36  
37      OutputStream _delegate = null;
38      ResponseSwitch _responseSwitch = null;
39  
40      public SwitchableOutputStream(ServletOutputStream delegate, ResponseSwitch responseSwitch)
41      {
42          _delegate = delegate;
43          _responseSwitch = responseSwitch;
44      }
45  
46      @Override
47      public void close() throws IOException
48      {
49          if (_responseSwitch.isEnabled())
50          {
51              _delegate.close();
52          }
53      }
54  
55      @Override
56      public void flush() throws IOException
57      {
58          if (_responseSwitch.isEnabled())
59          {
60              _delegate.flush();
61          }
62      }
63  
64      @Override
65      public void write(byte[] b, int off, int len) throws IOException
66      {
67          if (_responseSwitch.isEnabled())
68          {
69              _delegate.write(b, off, len);
70          }
71      }
72  
73      @Override
74      public void write(byte[] b) throws IOException
75      {
76          if (_responseSwitch.isEnabled())
77          {
78              _delegate.write(b);
79          }
80      }
81  
82      @Override
83      public void write(int b) throws IOException
84      {
85          if (_responseSwitch.isEnabled())
86          {
87              _delegate.write(b);
88          }
89      }
90      
91  }