/* * 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 compliance with * the License. You may obtain a copy of the License at * * https://www.apache.org/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 org.apache.commons.io.input; import static org.apache.commons.io.IOUtils.EOF; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; import org.apache.commons.io.build.AbstractStreamBuilder; import org.apache.commons.io.function.Erase; import org.apache.commons.io.function.IOConsumer; import org.apache.commons.io.function.IOIntConsumer; /** * A proxy stream which acts as a {@link FilterInputStream}, by passing all method calls on to the proxied stream, not changing which methods are called. *
* It is an alternative base class to {@link FilterInputStream} to increase reusability, because {@link FilterInputStream} changes the methods being called, * such as read(byte[]) to read(byte[], int, int). *
** In addition, this class allows you to: *
** Setting this value causes the {@link ProxyInputStream#afterRead(int) afterRead} method to delegate to the given consumer. *
** If a subclass overrides {@link ProxyInputStream#afterRead(int) afterRead} and does not call {@code super.afterRead(int)}, then the given consumer is * not called. *
** This does not override a {@code ProxyInputStream} subclass' implementation of the {@link ProxyInputStream#afterRead(int)} method, it can * supplement it. *
* * @param afterRead the {@link ProxyInputStream#afterRead(int)} behavior. * @return this instance. */ public B setAfterRead(final IOIntConsumer afterRead) { this.afterRead = afterRead; return asThis(); } } /** * Tracks whether {@link #close()} has been called or not. */ private volatile boolean closed; /** * Handles exceptions. */ private final IOConsumer* The default delegates to the consumer given to {@link 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 {@link #skip(long)} or {@link #reset()}. You need to explicitly override those methods if you want to add * post-processing steps also to them. *
* * @param n number of bytes read, or {@link IOUtils#EOF EOF} if the end of stream was reached. * @throws IOException Thrown by a subclass or the consumer given to {@link AbstractBuilder#setAfterRead(IOIntConsumer)}. * @since 2.0 */ protected void afterRead(final int n) throws IOException { afterRead.accept(n); } /** * Invokes the delegate's {@link InputStream#available()} method. * * @return the number of available bytes, 0 if the stream is closed. * @throws IOException if an I/O error occurs. */ @Override public int available() throws IOException { if (in != null && !isClosed()) { try { return in.available(); } catch (final IOException e) { handleIOException(e); } } return 0; } /** * Invoked by the {@code 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 common pre-processing * functionality without having to override all the read methods. * The default 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. *
* * @param n number of bytes that the caller asked to be read. * @throws IOException if the pre-processing fails in a subclass. * @since 2.0 */ @SuppressWarnings("unused") // Possibly thrown from subclasses. protected void beforeRead(final int n) throws IOException { // no-op default } /** * Checks if this instance is closed and throws an IOException if so. * * @throws IOException if this instance is closed. */ void checkOpen() throws IOException { Input.checkOpen(!isClosed()); } /** * Invokes the delegate's {@link InputStream#close()} method. * * @throws IOException if an I/O error occurs. */ @Override public void close() throws IOException { IOUtils.close(in, this::handleIOException); closed = true; } /** * 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. *
* * @param e The IOException thrown. * @throws IOException if an I/O error occurs. * @since 2.0 */ protected void handleIOException(final IOException e) throws IOException { exceptionHandler.accept(e); } /** * Tests whether this instance is closed. * * @return whether this instance is closed. */ boolean isClosed() { return closed; } /** * Invokes the delegate's {@link InputStream#mark(int)} method. * * @param readLimit read ahead limit. */ @Override public synchronized void mark(final int readLimit) { if (in != null) { in.mark(readLimit); } } /** * Invokes the delegate's {@link InputStream#markSupported()} method. * * @return {@code true} if this stream instance supports the mark and reset methods; {@code false} otherwise. * @see #mark(int) * @see #reset() */ @Override public boolean markSupported() { return in != null && in.markSupported(); } /** * Invokes the delegate's {@link InputStream#read()} method unless the stream is closed. * * @return the byte read or {@link IOUtils#EOF EOF} if we reached the end of stream. * @throws IOException if an I/O error occurs. */ @Override public int read() throws IOException { try { beforeRead(1); final int b = in.read(); afterRead(b != EOF ? 1 : EOF); return b; } catch (final IOException e) { handleIOException(e); return EOF; } } /** * Invokes the delegate's {@link InputStream#read(byte[])} method. * * @param b the buffer to read the bytes into. * @return the number of bytes read or {@link IOUtils#EOF EOF} if we reached the end of stream. * @throws IOException ** Use with caution; useful to query the underlying {@link InputStream}. *
* * @return the underlying {@link InputStream}. * @since 2.16.0 */ public InputStream unwrap() { return in; } }