Class ProxyInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.ProxyInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
AutoCloseInputStream,BOMInputStream,BoundedInputStream,CloseShieldInputStream,CountingInputStream,MarkShieldInputStream,ObservableInputStream,SwappedDataInputStream,TaggedInputStream,TeeInputStream
public abstract class ProxyInputStream extends java.io.FilterInputStream
A proxy stream which acts as aFilterInputStream, by passing all method calls on to the proxied stream, not changing which methods are called.It is an alternative base class to
FilterInputStreamto increase reusability, becauseFilterInputStreamchanges the methods being called, such as read(byte[]) to read(byte[], int, int).In addition, this class allows you to:
- notify a subclass that n bytes are about to be read through
beforeRead(int) - notify a subclass that n bytes were read through
afterRead(int) - notify a subclass that an exception was caught through
handleIOException(IOException) unwrap()itself
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classProxyInputStream.AbstractBuilder<T,B extends AbstractStreamBuilder<T,B>>Abstracts builder properties for subclasses.
-
Constructor Summary
Constructors Modifier Constructor Description ProxyInputStream(java.io.InputStream proxy)Constructs a new ProxyInputStream.protectedProxyInputStream(java.io.InputStream proxy, ProxyInputStream.AbstractBuilder<?,?> builder)Constructs a new ProxyInputStream.protectedProxyInputStream(ProxyInputStream.AbstractBuilder<?,?> builder)Constructs a new ProxyInputStream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterRead(int n)Called by thereadmethods after the proxied call has returned successfully.intavailable()Invokes the delegate'sInputStream.available()method.protected voidbeforeRead(int n)Invoked by thereadmethods before the call is proxied.voidclose()Invokes the delegate'sInputStream.close()method.protected voidhandleIOException(java.io.IOException e)Handles any IOExceptions thrown; by default, throws the given exception.voidmark(int readLimit)Invokes the delegate'sInputStream.mark(int)method.booleanmarkSupported()Invokes the delegate'sInputStream.markSupported()method.intread()Invokes the delegate'sInputStream.read()method unless the stream is closed.intread(byte[] b)Invokes the delegate'sInputStream.read(byte[])method.intread(byte[] b, int off, int len)Invokes the delegate'sInputStream.read(byte[], int, int)method.voidreset()Invokes the delegate'sInputStream.reset()method.ProxyInputStreamsetReference(java.io.InputStream in)Sets the underlying input stream.longskip(long n)Invokes the delegate'sInputStream.skip(long)method.java.io.InputStreamunwrap()Unwraps this instance by returning the underlyingInputStream.
-
-
-
Constructor Detail
-
ProxyInputStream
protected ProxyInputStream(ProxyInputStream.AbstractBuilder<?,?> builder) throws java.io.IOException
Constructs a new ProxyInputStream.- Parameters:
builder- How to build an instance.- Throws:
java.io.IOException- if an I/O error occurs.- Since:
- 2.18.0
-
ProxyInputStream
public ProxyInputStream(java.io.InputStream proxy)
Constructs a new ProxyInputStream.- Parameters:
proxy- the InputStream to proxy.
-
ProxyInputStream
protected ProxyInputStream(java.io.InputStream proxy, ProxyInputStream.AbstractBuilder<?,?> builder)
Constructs a new ProxyInputStream.- Parameters:
proxy- the InputStream to proxy.builder- How to build an instance.- Since:
- 2.18.0
-
-
Method Detail
-
afterRead
protected void afterRead(int n) throws java.io.IOException
Called by thereadmethods after the proxied call has returned successfully. The argument is the number of bytes returned to the caller orEOFif the end of stream was reached.The default delegates to the consumer given to
ProxyInputStream.AbstractBuilder.setAfterRead(IOIntConsumer).Alternatively, a subclasses can override this method to add post-processing functionality without having to override all the read methods.
Note this method is not called from
skip(long)orreset(). You need to explicitly override those methods if you want to add post-processing steps also to them.- Parameters:
n- number of bytes read, orEOFif the end of stream was reached.- Throws:
java.io.IOException- Thrown by a subclass or the consumer given toProxyInputStream.AbstractBuilder.setAfterRead(IOIntConsumer).- Since:
- 2.0
-
available
public int available() throws java.io.IOException
Invokes the delegate'sInputStream.available()method.- Overrides:
availablein classjava.io.FilterInputStream- Returns:
- the number of available bytes, 0 if the stream is closed.
- Throws:
java.io.IOException- if an I/O error occurs.
-
beforeRead
protected void beforeRead(int n) throws java.io.IOException
Invoked by thereadmethods before the call is proxied. The number of bytes that the caller wanted to read (1 for theread()method, buffer length forread(byte[]), etc.) is given as an argument.Subclasses can override this method to add common pre-processing functionality without having to override all the read methods. The default implementation does nothing.
Note this method is not called from
skip(long)orreset(). You need to explicitly override those methods if you want to add pre-processing steps also to them.- Parameters:
n- number of bytes that the caller asked to be read.- Throws:
java.io.IOException- if the pre-processing fails in a subclass.- Since:
- 2.0
-
close
public void close() throws java.io.IOException
Invokes the delegate'sInputStream.close()method.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.FilterInputStream- Throws:
java.io.IOException- if an I/O error occurs.
-
handleIOException
protected void handleIOException(java.io.IOException e) throws java.io.IOException
Handles any IOExceptions thrown; by default, throws the given exception.This method provides a point to implement custom exception handling. The default behavior is to re-throw the exception.
- Parameters:
e- The IOException thrown.- Throws:
java.io.IOException- if an I/O error occurs.- Since:
- 2.0
-
mark
public void mark(int readLimit)
Invokes the delegate'sInputStream.mark(int)method.- Overrides:
markin classjava.io.FilterInputStream- Parameters:
readLimit- read ahead limit.
-
markSupported
public boolean markSupported()
Invokes the delegate'sInputStream.markSupported()method.
-
read
public int read() throws java.io.IOException
Invokes the delegate'sInputStream.read()method unless the stream is closed.- Overrides:
readin classjava.io.FilterInputStream- Returns:
- the byte read or
EOFif we reached the end of stream. - Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOException
Invokes the delegate'sInputStream.read(byte[])method.- Overrides:
readin classjava.io.FilterInputStream- Parameters:
b- the buffer to read the bytes into.- Returns:
- the number of bytes read or
EOFif we reached the end of stream. - Throws:
java.io.IOException-- If the first byte cannot be read for any reason other than the end of the file,
- if the input stream has been closed, or
- if some other I/O error occurs.
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
Invokes the delegate'sInputStream.read(byte[], int, int)method.- Overrides:
readin classjava.io.FilterInputStream- Parameters:
b- the buffer to read the bytes into.off- The start offset.len- The number of bytes to read.- Returns:
- the number of bytes read or
EOFif we reached the end of stream. - Throws:
java.io.IOException-- If the first byte cannot be read for any reason other than the end of the file,
- if the input stream has been closed, or
- if some other I/O error occurs.
-
reset
public void reset() throws java.io.IOException
Invokes the delegate'sInputStream.reset()method.- Overrides:
resetin classjava.io.FilterInputStream- Throws:
java.io.IOException- if this stream has not been marked or if the mark has been invalidated.
-
setReference
public ProxyInputStream setReference(java.io.InputStream in)
Sets the underlying input stream.- Parameters:
in- The input stream to set inFilterInputStream.in.- Returns:
thisinstance.- Since:
- 2.19.0
-
skip
public long skip(long n) throws java.io.IOException
Invokes the delegate'sInputStream.skip(long)method.- Overrides:
skipin classjava.io.FilterInputStream- Parameters:
n- the number of bytes to skip.- Returns:
- the actual number of bytes skipped.
- Throws:
java.io.IOException- if the stream does not support seek, or if some other I/O error occurs.
-
unwrap
public java.io.InputStream unwrap()
Unwraps this instance by returning the underlyingInputStream.Use with caution; useful to query the underlying
InputStream.- Returns:
- the underlying
InputStream. - Since:
- 2.16.0
-
-