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.input;
18  
19  import java.io.FilterReader;
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.nio.CharBuffer;
23  
24  /**
25   * A Proxy stream which acts as expected, that is it passes the method 
26   * calls on to the proxied stream and doesn't change which methods are 
27   * being called. 
28   * <p>
29   * It is an alternative base class to FilterReader
30   * to increase reusability, because FilterReader changes the 
31   * methods being called, such as read(char[]) to read(char[], int, int).
32   * 
33   * @author Stephen Colebourne
34   * @version $Id: ProxyReader.java 619135 2008-02-06 20:09:02Z niallp $
35   */
36  public abstract class ProxyReader extends FilterReader {
37  
38      /**
39       * Constructs a new ProxyReader.
40       * 
41       * @param proxy  the Reader to delegate to
42       */
43      public ProxyReader(Reader proxy) {
44          super(proxy);
45          // the proxy is stored in a protected superclass variable named 'in'
46      }
47  
48      /**
49       * Invokes the delegate's <code>read()</code> method.
50       * @return the character read or -1 if the end of stream
51       * @throws IOException if an I/O error occurs
52       */
53      public int read() throws IOException {
54          return in.read();
55      }
56  
57      /**
58       * Invokes the delegate's <code>read(char[])</code> method.
59       * @param chr the buffer to read the characters into
60       * @return the number of characters read or -1 if the end of stream
61       * @throws IOException if an I/O error occurs
62       */
63      public int read(char[] chr) throws IOException {
64          return in.read(chr);
65      }
66  
67      /**
68       * Invokes the delegate's <code>read(char[], int, int)</code> method.
69       * @param chr the buffer to read the characters into
70       * @param st The start offset
71       * @param end The number of bytes to read
72       * @return the number of characters read or -1 if the end of stream
73       * @throws IOException if an I/O error occurs
74       */
75      public int read(char[] chr, int st, int end) throws IOException {
76          return in.read(chr, st, end);
77      }
78  
79      /**
80       * Invokes the delegate's <code>read(CharBuffer)</code> method.
81       * @param target the char buffer to read the characters into
82       * @return the number of characters read or -1 if the end of stream
83       * @throws IOException if an I/O error occurs
84       * @since IO 2.0
85       */
86      public int read(CharBuffer target) throws IOException {
87          return in.read(target);
88      }
89  
90      /**
91       * Invokes the delegate's <code>skip(long)</code> method.
92       * @param ln the number of bytes to skip
93       * @return the number of bytes to skipped or -1 if the end of stream
94       * @throws IOException if an I/O error occurs
95       */
96      public long skip(long ln) throws IOException {
97          return in.skip(ln);
98      }
99  
100     /**
101      * Invokes the delegate's <code>ready()</code> method.
102      * @return true if the stream is ready to be read
103      * @throws IOException if an I/O error occurs
104      */
105     public boolean ready() throws IOException {
106         return in.ready();
107     }
108 
109     /**
110      * Invokes the delegate's <code>close()</code> method.
111      * @throws IOException if an I/O error occurs
112      */
113     public void close() throws IOException {
114         in.close();
115     }
116 
117     /**
118      * Invokes the delegate's <code>mark(int)</code> method.
119      * @param idx read ahead limit
120      * @throws IOException if an I/O error occurs
121      */
122     public synchronized void mark(int idx) throws IOException {
123         in.mark(idx);
124     }
125 
126     /**
127      * Invokes the delegate's <code>reset()</code> method.
128      * @throws IOException if an I/O error occurs
129      */
130     public synchronized void reset() throws IOException {
131         in.reset();
132     }
133 
134     /**
135      * Invokes the delegate's <code>markSupported()</code> method.
136      * @return true if mark is supported, otherwise false
137      */
138     public boolean markSupported() {
139         return in.markSupported();
140     }
141 
142 }