View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.output;
18  
19  import java.io.FilterWriter;
20  import java.io.IOException;
21  import java.io.Writer;
22  
23  /**
24   * A Proxy stream which acts as expected, that is it passes the method 
25   * calls on to the proxied stream and doesn't change which methods are 
26   * being called. It is an alternative base class to FilterWriter
27   * to increase reusability, because FilterWriter changes the 
28   * methods being called, such as write(char[]) to write(char[], int, int)
29   * and write(String) to write(String, int, int).
30   * 
31   * @author Stephen Colebourne
32   * @version $Id: ProxyWriter.java 619144 2008-02-06 20:23:25Z niallp $
33   */
34  public class ProxyWriter extends FilterWriter {
35  
36      /**
37       * Constructs a new ProxyWriter.
38       * 
39       * @param proxy  the Writer to delegate to
40       */
41      public ProxyWriter(Writer proxy) {
42          super(proxy);
43          // the proxy is stored in a protected superclass variable named 'out'
44      }
45  
46      /**
47       * Invokes the delegate's <code>append(char)</code> method.
48       * @param c The character to write
49       * @return this writer
50       * @throws IOException if an I/O error occurs
51       * @since IO 2.0
52       */
53      public Writer append(char c) throws IOException {
54          out.append(c);
55          return this;
56      }
57  
58      /**
59       * Invokes the delegate's <code>append(CharSequence, int, int)</code> method.
60       * @param csq The character sequence to write
61       * @param start The index of the first character to write
62       * @param end  The index of the first character to write (exclusive)
63       * @return this writer
64       * @throws IOException if an I/O error occurs
65       * @since IO 2.0
66       */
67      public Writer append(CharSequence csq, int start, int end) throws IOException {
68          out.append(csq, start, end);
69          return this;
70      }
71  
72      /**
73       * Invokes the delegate's <code>append(CharSequence)</code> method.
74       * @param csq The character sequence to write
75       * @return this writer
76       * @throws IOException if an I/O error occurs
77       * @since IO 2.0
78       */
79      public Writer append(CharSequence csq) throws IOException {
80          out.append(csq);
81          return this;
82      }
83  
84      /**
85       * Invokes the delegate's <code>write(int)</code> method.
86       * @param idx the character to write
87       * @throws IOException if an I/O error occurs
88       */
89      public void write(int idx) throws IOException {
90          out.write(idx);
91      }
92  
93      /**
94       * Invokes the delegate's <code>write(char[])</code> method.
95       * @param chr the characters to write
96       * @throws IOException if an I/O error occurs
97       */
98      public void write(char[] chr) throws IOException {
99          out.write(chr);
100     }
101 
102     /**
103      * Invokes the delegate's <code>write(char[], int, int)</code> method.
104      * @param chr the characters to write
105      * @param st The start offset
106      * @param end The number of characters to write
107      * @throws IOException if an I/O error occurs
108      */
109     public void write(char[] chr, int st, int end) throws IOException {
110         out.write(chr, st, end);
111     }
112 
113     /**
114      * Invokes the delegate's <code>write(String)</code> method.
115      * @param str the string to write
116      * @throws IOException if an I/O error occurs
117      */
118     public void write(String str) throws IOException {
119         out.write(str);
120     }
121 
122     /**
123      * Invokes the delegate's <code>write(String)</code> method.
124      * @param str the string to write
125      * @param st The start offset
126      * @param end The number of characters to write
127      * @throws IOException if an I/O error occurs
128      */
129     public void write(String str, int st, int end) throws IOException {
130         out.write(str, st, end);
131     }
132 
133     /**
134      * Invokes the delegate's <code>flush()</code> method.
135      * @throws IOException if an I/O error occurs
136      */
137     public void flush() throws IOException {
138         out.flush();
139     }
140 
141     /**
142      * Invokes the delegate's <code>close()</code> method.
143      * @throws IOException if an I/O error occurs
144      */
145     public void close() throws IOException {
146         out.close();
147     }
148 
149 }