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 */
17package org.apache.commons.io.input;
18
19import java.io.FilterInputStream;
20import java.io.IOException;
21import java.io.InputStream;
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.
27 * <p>
28 * It is an alternative base class to FilterInputStream
29 * to increase reusability, because FilterInputStream changes the
30 * methods being called, such as read(byte[]) to read(byte[], int, int).
31 *
32 * @author Stephen Colebourne
33 * @version $Id: ProxyInputStream.java 610010 2008-01-08 14:50:59Z niallp $
34 */
35public abstract class ProxyInputStream extends FilterInputStream {
36
37    /**
38     * Constructs a new ProxyInputStream.
39     *
40     * @param proxy  the InputStream to delegate to
41     */
42    public ProxyInputStream(InputStream proxy) {
43        super(proxy);
44        // the proxy is stored in a protected superclass variable named 'in'
45    }
46
47    /**
48     * Invokes the delegate's <code>read()</code> method.
49     * @return the byte read or -1 if the end of stream
50     * @throws IOException if an I/O error occurs
51     */
52    public int read() throws IOException {
53        return in.read();
54    }
55
56    /**
57     * Invokes the delegate's <code>read(byte[])</code> method.
58     * @param bts the buffer to read the bytes into
59     * @return the number of bytes read or -1 if the end of stream
60     * @throws IOException if an I/O error occurs
61     */
62    public int read(byte[] bts) throws IOException {
63        return in.read(bts);
64    }
65
66    /**
67     * Invokes the delegate's <code>read(byte[], int, int)</code> method.
68     * @param bts the buffer to read the bytes into
69     * @param st The start offset
70     * @param end The number of bytes to read
71     * @return the number of bytes read or -1 if the end of stream
72     * @throws IOException if an I/O error occurs
73     */
74    public int read(byte[] bts, int st, int end) throws IOException {
75        return in.read(bts, st, end);
76    }
77
78    /**
79     * Invokes the delegate's <code>skip(long)</code> method.
80     * @param ln the number of bytes to skip
81     * @return the number of bytes to skipped or -1 if the end of stream
82     * @throws IOException if an I/O error occurs
83     */
84    public long skip(long ln) throws IOException {
85        return in.skip(ln);
86    }
87
88    /**
89     * Invokes the delegate's <code>available()</code> method.
90     * @return the number of available bytes
91     * @throws IOException if an I/O error occurs
92     */
93    public int available() throws IOException {
94        return in.available();
95    }
96
97    /**
98     * Invokes the delegate's <code>close()</code> method.
99     * @throws IOException if an I/O error occurs
100     */
101    public void close() throws IOException {
102        in.close();
103    }
104
105    /**
106     * Invokes the delegate's <code>mark(int)</code> method.
107     * @param idx read ahead limit
108     */
109    public synchronized void mark(int idx) {
110        in.mark(idx);
111    }
112
113    /**
114     * Invokes the delegate's <code>reset()</code> method.
115     * @throws IOException if an I/O error occurs
116     */
117    public synchronized void reset() throws IOException {
118        in.reset();
119    }
120
121    /**
122     * Invokes the delegate's <code>markSupported()</code> method.
123     * @return true if mark is supported, otherwise false
124     */
125    public boolean markSupported() {
126        return in.markSupported();
127    }
128
129}
130