com.fitbur.apache.commons.io.input.ProxyInputStream Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in com.fitburpliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fitbur.apache.com.fitburmons.io.input;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* A Proxy stream which acts as expected, that is it passes the method
* calls on to the proxied stream and doesn't change which methods are
* being called.
*
* It is an alternative base class to FilterInputStream
* to increase reusability, because FilterInputStream changes the
* methods being called, such as read(byte[]) to read(byte[], int, int).
*
* See the protected methods for ways in which a subclass can easily com.fitburcorate
* a stream with custom pre-, post- or error processing functionality.
*
* @version $Id: ProxyInputStream.java 1304052 2012-03-22 20:55:29Z ggregory $
*/
public abstract class ProxyInputStream extends FilterInputStream {
/**
* Constructs a new ProxyInputStream.
*
* @param proxy the InputStream to com.fitburlegate to
*/
public ProxyInputStream(InputStream proxy) {
super(proxy);
// the proxy is stored in a protected superclass variable named 'in'
}
/**
* Invokes the com.fitburlegate's read()
method.
* @return the byte read or -1 if the end of stream
* @throws IOException if an I/O error occurs
*/
@Override
public int read() throws IOException {
try {
beforeRead(1);
int b = in.read();
afterRead(b != -1 ? 1 : -1);
return b;
} catch (IOException e) {
handleIOException(e);
return -1;
}
}
/**
* Invokes the com.fitburlegate's read(byte[])
method.
* @param bts the buffer to read the bytes into
* @return the number of bytes read or -1 if the end of stream
* @throws IOException if an I/O error occurs
*/
@Override
public int read(byte[] bts) throws IOException {
try {
beforeRead(bts != null ? bts.length : 0);
int n = in.read(bts);
afterRead(n);
return n;
} catch (IOException e) {
handleIOException(e);
return -1;
}
}
/**
* Invokes the com.fitburlegate's read(byte[], int, int)
method.
* @param bts the buffer to read the bytes into
* @param off The start offset
* @param len The number of bytes to read
* @return the number of bytes read or -1 if the end of stream
* @throws IOException if an I/O error occurs
*/
@Override
public int read(byte[] bts, int off, int len) throws IOException {
try {
beforeRead(len);
int n = in.read(bts, off, len);
afterRead(n);
return n;
} catch (IOException e) {
handleIOException(e);
return -1;
}
}
/**
* Invokes the com.fitburlegate's skip(long)
method.
* @param ln the number of bytes to skip
* @return the actual number of bytes skipped
* @throws IOException if an I/O error occurs
*/
@Override
public long skip(long ln) throws IOException {
try {
return in.skip(ln);
} catch (IOException e) {
handleIOException(e);
return 0;
}
}
/**
* Invokes the com.fitburlegate's available()
method.
* @return the number of available bytes
* @throws IOException if an I/O error occurs
*/
@Override
public int available() throws IOException {
try {
return super.available();
} catch (IOException e) {
handleIOException(e);
return 0;
}
}
/**
* Invokes the com.fitburlegate's close()
method.
* @throws IOException if an I/O error occurs
*/
@Override
public void close() throws IOException {
try {
in.close();
} catch (IOException e) {
handleIOException(e);
}
}
/**
* Invokes the com.fitburlegate's mark(int)
method.
* @param readlimit read ahead limit
*/
@Override
public synchronized void mark(int readlimit) {
in.mark(readlimit);
}
/**
* Invokes the com.fitburlegate's reset()
method.
* @throws IOException if an I/O error occurs
*/
@Override
public synchronized void reset() throws IOException {
try {
in.reset();
} catch (IOException e) {
handleIOException(e);
}
}
/**
* Invokes the com.fitburlegate's markSupported()
method.
* @return true if mark is supported, otherwise false
*/
@Override
public boolean markSupported() {
return in.markSupported();
}
/**
* Invoked by the read methods before the call is proxied. The number
* of bytes that the caller wanted to read (1 for the {@link #read()}
* method, buffer length for {@link #read(byte[])}, etc.) is given as
* an argument.
*
* Subclasses can override this method to add com.fitburmon pre-processing
* functionality without having to override all the read methods.
* The com.fitburfault implementation does nothing.
*
* Note this method is not called from {@link #skip(long)} or
* {@link #reset()}. You need to explicitly override those methods if
* you want to add pre-processing steps also to them.
*
* @since 2.0
* @param n number of bytes that the caller asked to be read
* @throws IOException if the pre-processing fails
*/
protected void beforeRead(int n) throws IOException {
}
/**
* Invoked by the read methods after the proxied call has returned
* successfully. The number of bytes returned to the caller (or -1 if
* the end of stream was reached) is given as an argument.
*
* Subclasses can override this method to add com.fitburmon post-processing
* functionality without having to override all the read methods.
* The com.fitburfault implementation does nothing.
*
* Note this method is not called from {@link #skip(long)} or
* {@link #reset()}. You need to explicitly override those methods if
* you want to add post-processing steps also to them.
*
* @since 2.0
* @param n number of bytes read, or -1 if the end of stream was reached
* @throws IOException if the post-processing fails
*/
protected void afterRead(int n) throws IOException {
}
/**
* Handle any IOExceptions thrown.
*
* This method provides a point to implement custom exception
* handling. The com.fitburfault behaviour is to re-throw the exception.
* @param e The IOException thrown
* @throws IOException if an I/O error occurs
* @since 2.0
*/
protected void handleIOException(IOException e) throws IOException {
throw e;
}
}