public abstract class ProxyInputStream extends FilterInputStream
FilterInputStream, by passing all method calls on to the proxied stream, not changing which methods are 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).
In addition, this class allows you to:
beforeRead(int)afterRead(int)handleIOException(IOException)unwrap() itself| Modifier and Type | Class and Description |
|---|---|
protected static class |
ProxyInputStream.AbstractBuilder<T,B extends AbstractStreamBuilder<T,B>>
Abstracts builder properties for subclasses.
|
in| Modifier | Constructor and Description |
|---|---|
|
ProxyInputStream(InputStream proxy)
Constructs a new ProxyInputStream.
|
protected |
ProxyInputStream(InputStream proxy,
ProxyInputStream.AbstractBuilder<?,?> builder)
Constructs a new ProxyInputStream.
|
protected |
ProxyInputStream(ProxyInputStream.AbstractBuilder<?,?> builder)
Constructs a new ProxyInputStream.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
afterRead(int n)
Called by the
read methods after the proxied call has returned successfully. |
int |
available()
Invokes the delegate's
InputStream.available() method. |
protected void |
beforeRead(int n)
Invoked by the
read methods before the call is proxied. |
(package private) void |
checkOpen()
Checks if this instance is closed and throws an IOException if so.
|
void |
close()
Invokes the delegate's
InputStream.close() method. |
protected void |
handleIOException(IOException e)
Handles any IOExceptions thrown; by default, throws the given exception.
|
(package private) boolean |
isClosed()
Tests whether this instance is closed.
|
void |
mark(int readLimit)
Invokes the delegate's
InputStream.mark(int) method. |
boolean |
markSupported()
Invokes the delegate's
InputStream.markSupported() method. |
int |
read()
Invokes the delegate's
InputStream.read() method unless the stream is closed. |
int |
read(byte[] b)
Invokes the delegate's
InputStream.read(byte[]) method. |
int |
read(byte[] b,
int off,
int len)
Invokes the delegate's
InputStream.read(byte[], int, int) method. |
void |
reset()
Invokes the delegate's
InputStream.reset() method. |
ProxyInputStream |
setReference(InputStream in)
Sets the underlying input stream.
|
long |
skip(long n)
Invokes the delegate's
InputStream.skip(long) method. |
InputStream |
unwrap()
Unwraps this instance by returning the underlying
InputStream. |
public ProxyInputStream(InputStream proxy)
proxy - the InputStream to proxy.protected ProxyInputStream(InputStream proxy, ProxyInputStream.AbstractBuilder<?,?> builder)
proxy - the InputStream to proxy.builder - How to build an instance.protected ProxyInputStream(ProxyInputStream.AbstractBuilder<?,?> builder) throws IOException
builder - How to build an instance.IOException - if an I/O error occurs.protected void afterRead(int n)
throws IOException
read methods after the proxied call has returned successfully. The argument is the number of bytes returned to the caller or
EOF if 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) or reset(). You need to explicitly override those methods if you want to add
post-processing steps also to them.
n - number of bytes read, or EOF if the end of stream was reached.IOException - Thrown by a subclass or the consumer given to ProxyInputStream.AbstractBuilder.setAfterRead(IOIntConsumer).public int available()
throws IOException
InputStream.available() method.available in class FilterInputStreamIOException - if an I/O error occurs.protected void beforeRead(int n)
throws IOException
read methods before the call is proxied. The number
of bytes that the caller wanted to read (1 for the read()
method, buffer length for read(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) or
reset(). You need to explicitly override those methods if
you want to add pre-processing steps also to them.
n - number of bytes that the caller asked to be read.IOException - if the pre-processing fails in a subclass.void checkOpen()
throws IOException
IOException - if this instance is closed.public void close()
throws IOException
InputStream.close() method.close in interface Closeableclose in interface AutoCloseableclose in class FilterInputStreamIOException - if an I/O error occurs.protected void handleIOException(IOException e) throws IOException
This method provides a point to implement custom exception handling. The default behavior is to re-throw the exception.
e - The IOException thrown.IOException - if an I/O error occurs.boolean isClosed()
public void mark(int readLimit)
InputStream.mark(int) method.mark in class FilterInputStreamreadLimit - read ahead limit.public boolean markSupported()
InputStream.markSupported() method.markSupported in class FilterInputStreamtrue if this stream instance supports the mark and reset methods; false otherwise.mark(int),
reset()public int read()
throws IOException
InputStream.read() method unless the stream is closed.read in class FilterInputStreamEOF if we reached the end of stream.IOException - if an I/O error occurs.public int read(byte[] b)
throws IOException
InputStream.read(byte[]) method.read in class FilterInputStreamb - the buffer to read the bytes into.EOF if we reached the end of stream.IOException - public int read(byte[] b,
int off,
int len)
throws IOException
InputStream.read(byte[], int, int) method.read in class FilterInputStreamb - the buffer to read the bytes into.off - The start offset.len - The number of bytes to read.EOF if we reached the end of stream.IOException - public void reset()
throws IOException
InputStream.reset() method.reset in class FilterInputStreamIOException - if this stream has not been marked or if the mark has been invalidated.public ProxyInputStream setReference(InputStream in)
in - The input stream to set in FilterInputStream.in.public long skip(long n)
throws IOException
InputStream.skip(long) method.skip in class FilterInputStreamn - the number of bytes to skip.IOException - if the stream does not support seek, or if some other I/O error occurs.public InputStream unwrap()
InputStream.
Use with caution; useful to query the underlying InputStream.
InputStream.