001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.io.input; 018 019 import java.io.FilterInputStream; 020 import java.io.IOException; 021 import java.io.InputStream; 022 023 /** 024 * A Proxy stream which acts as expected, that is it passes the method 025 * calls on to the proxied stream and doesn't change which methods are 026 * being called. 027 * <p> 028 * It is an alternative base class to FilterInputStream 029 * to increase reusability, because FilterInputStream changes the 030 * methods being called, such as read(byte[]) to read(byte[], int, int). 031 * 032 * @author Stephen Colebourne 033 * @version $Id: ProxyInputStream.java 610010 2008-01-08 14:50:59Z niallp $ 034 */ 035 public abstract class ProxyInputStream extends FilterInputStream { 036 037 /** 038 * Constructs a new ProxyInputStream. 039 * 040 * @param proxy the InputStream to delegate to 041 */ 042 public ProxyInputStream(InputStream proxy) { 043 super(proxy); 044 // the proxy is stored in a protected superclass variable named 'in' 045 } 046 047 /** 048 * Invokes the delegate's <code>read()</code> method. 049 * @return the byte read or -1 if the end of stream 050 * @throws IOException if an I/O error occurs 051 */ 052 public int read() throws IOException { 053 return in.read(); 054 } 055 056 /** 057 * Invokes the delegate's <code>read(byte[])</code> method. 058 * @param bts the buffer to read the bytes into 059 * @return the number of bytes read or -1 if the end of stream 060 * @throws IOException if an I/O error occurs 061 */ 062 public int read(byte[] bts) throws IOException { 063 return in.read(bts); 064 } 065 066 /** 067 * Invokes the delegate's <code>read(byte[], int, int)</code> method. 068 * @param bts the buffer to read the bytes into 069 * @param st The start offset 070 * @param end The number of bytes to read 071 * @return the number of bytes read or -1 if the end of stream 072 * @throws IOException if an I/O error occurs 073 */ 074 public int read(byte[] bts, int st, int end) throws IOException { 075 return in.read(bts, st, end); 076 } 077 078 /** 079 * Invokes the delegate's <code>skip(long)</code> method. 080 * @param ln the number of bytes to skip 081 * @return the number of bytes to skipped or -1 if the end of stream 082 * @throws IOException if an I/O error occurs 083 */ 084 public long skip(long ln) throws IOException { 085 return in.skip(ln); 086 } 087 088 /** 089 * Invokes the delegate's <code>available()</code> method. 090 * @return the number of available bytes 091 * @throws IOException if an I/O error occurs 092 */ 093 public int available() throws IOException { 094 return in.available(); 095 } 096 097 /** 098 * Invokes the delegate's <code>close()</code> method. 099 * @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 }