This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new a304a37 Internal refactroring. Javadoc. Sort members. a304a37 is described below commit a304a37c7f8c4f745bab92393b08f11c4b403830 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Fri Jan 29 08:58:24 2021 -0500 Internal refactroring. Javadoc. Sort members. --- .../commons/io/input/ObservableInputStream.java | 220 ++++++++++----------- 1 file changed, 109 insertions(+), 111 deletions(-) diff --git a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java index c287dae..6d5fd2a 100644 --- a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java @@ -42,15 +42,12 @@ public class ObservableInputStream extends ProxyInputStream { public static abstract class Observer { /** - * Called to indicate, that {@link InputStream#read()} has been invoked on the {@link ObservableInputStream}, - * and will return a value. + * Called to indicate that the {@link ObservableInputStream} has been closed. * - * @param value The value, which is being returned. This will never be -1 (EOF), because, in that case, - * {@link #finished()} will be invoked instead. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("unused") // Possibly thrown from subclasses. - public void data(final int value) throws IOException { + public void closed() throws IOException { // noop } @@ -69,34 +66,37 @@ public class ObservableInputStream extends ProxyInputStream { } /** - * Called to indicate that EOF has been seen on the underlying stream. This method may be called multiple times, - * if the reader keeps invoking either of the read methods, and they will consequently keep returning EOF. + * Called to indicate, that {@link InputStream#read()} has been invoked on the {@link ObservableInputStream}, + * and will return a value. * + * @param value The value, which is being returned. This will never be -1 (EOF), because, in that case, + * {@link #finished()} will be invoked instead. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("unused") // Possibly thrown from subclasses. - public void finished() throws IOException { + public void data(final int value) throws IOException { // noop } /** - * Called to indicate that the {@link ObservableInputStream} has been closed. + * Called to indicate that an error occurred on the underlying stream. * + * @param exception the exception to throw * @throws IOException if an I/O error occurs. */ - @SuppressWarnings("unused") // Possibly thrown from subclasses. - public void closed() throws IOException { - // noop + public void error(final IOException exception) throws IOException { + throw exception; } /** - * Called to indicate that an error occurred on the underlying stream. + * Called to indicate that EOF has been seen on the underlying stream. This method may be called multiple times, + * if the reader keeps invoking either of the read methods, and they will consequently keep returning EOF. * - * @param exception the exception to throw * @throws IOException if an I/O error occurs. */ - public void error(final IOException exception) throws IOException { - throw exception; + @SuppressWarnings("unused") // Possibly thrown from subclasses. + public void finished() throws IOException { + // noop } } @@ -120,91 +120,40 @@ public class ObservableInputStream extends ProxyInputStream { observers.add(observer); } - /** - * Removes an Observer. - * - * @param observer the observer to remove - */ - public void remove(final Observer observer) { - observers.remove(observer); - } - - /** - * Removes all Observers. - */ - public void removeAllObservers() { - observers.clear(); - } - @Override - public int read() throws IOException { - int result = 0; + public void close() throws IOException { IOException ioe = null; try { - result = super.read(); - } catch (final IOException pException) { - ioe = pException; + super.close(); + } catch (final IOException e) { + ioe = e; } - if (ioe != null) { - noteError(ioe); - } else if (result == EOF) { - noteFinished(); + if (ioe == null) { + noteClosed(); } else { - noteDataByte(result); - } - return result; - } - - @Override - public int read(final byte[] buffer) throws IOException { - int result = 0; - IOException ioe = null; - try { - result = super.read(buffer); - } catch (final IOException pException) { - ioe = pException; - } - if (ioe != null) { noteError(ioe); - } else if (result == EOF) { - noteFinished(); - } else if (result > 0) { - noteDataBytes(buffer, 0, result); } - return result; } - @Override - public int read(final byte[] buffer, final int offset, final int length) throws IOException { - int result = 0; - IOException ioe = null; - try { - result = super.read(buffer, offset, length); - } catch (final IOException pException) { - ioe = pException; - } - if (ioe != null) { - noteError(ioe); - } else if (result == EOF) { - noteFinished(); - } else if (result > 0) { - noteDataBytes(buffer, offset, result); + /** + * Reads all data from the underlying {@link InputStream}, while notifying the observers. + * + * @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception. + */ + public void consume() throws IOException { + final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE]; + while (read(buffer) != EOF) { + // empty } - return result; } /** - * Notifies the observers by invoking {@link Observer#data(byte[],int,int)} with the given arguments. + * Gets all currently registered observers. * - * @param buffer Passed to the observers. - * @param offset Passed to the observers. - * @param length Passed to the observers. - * @throws IOException Some observer has thrown an exception, which is being passed down. + * @return a list of the currently registered observers */ - protected void noteDataBytes(final byte[] buffer, final int offset, final int length) throws IOException { - for (final Observer observer : getObservers()) { - observer.data(buffer, offset, length); - } + protected List<Observer> getObservers() { + return observers; } /** @@ -212,9 +161,9 @@ public class ObservableInputStream extends ProxyInputStream { * * @throws IOException Some observer has thrown an exception, which is being passed down. */ - protected void noteFinished() throws IOException { + protected void noteClosed() throws IOException { for (final Observer observer : getObservers()) { - observer.finished(); + observer.closed(); } } @@ -231,6 +180,20 @@ public class ObservableInputStream extends ProxyInputStream { } /** + * Notifies the observers by invoking {@link Observer#data(byte[],int,int)} with the given arguments. + * + * @param buffer Passed to the observers. + * @param offset Passed to the observers. + * @param length Passed to the observers. + * @throws IOException Some observer has thrown an exception, which is being passed down. + */ + protected void noteDataBytes(final byte[] buffer, final int offset, final int length) throws IOException { + for (final Observer observer : getObservers()) { + observer.data(buffer, offset, length); + } + } + + /** * Notifies the observers by invoking {@link Observer#error(IOException)} with the given argument. * * @param exception Passed to the observers. @@ -248,46 +211,81 @@ public class ObservableInputStream extends ProxyInputStream { * * @throws IOException Some observer has thrown an exception, which is being passed down. */ - protected void noteClosed() throws IOException { + protected void noteFinished() throws IOException { for (final Observer observer : getObservers()) { - observer.closed(); + observer.finished(); } } - /** - * Gets all currently registered observers. - * - * @return a list of the currently registered observers - */ - protected List<Observer> getObservers() { - return observers; + private void notify(final byte[] buffer, final int offset, int result, IOException ioe) throws IOException { + if (ioe != null) { + noteError(ioe); + } else if (result == EOF) { + noteFinished(); + } else if (result > 0) { + noteDataBytes(buffer, offset, result); + } } @Override - public void close() throws IOException { + public int read() throws IOException { + int result = 0; IOException ioe = null; try { - super.close(); - } catch (final IOException e) { - ioe = e; + result = super.read(); + } catch (final IOException pException) { + ioe = pException; } - if (ioe == null) { - noteClosed(); - } else { + if (ioe != null) { noteError(ioe); + } else if (result == EOF) { + noteFinished(); + } else { + noteDataByte(result); + } + return result; + } + + @Override + public int read(final byte[] buffer) throws IOException { + int result = 0; + IOException ioe = null; + try { + result = super.read(buffer); + } catch (final IOException pException) { + ioe = pException; + } + notify(buffer, 0, result, ioe); + return result; + } + + @Override + public int read(final byte[] buffer, final int offset, final int length) throws IOException { + int result = 0; + IOException ioe = null; + try { + result = super.read(buffer, offset, length); + } catch (final IOException pException) { + ioe = pException; } + notify(buffer, offset, result, ioe); + return result; } /** - * Reads all data from the underlying {@link InputStream}, while notifying the observers. + * Removes an Observer. * - * @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception. + * @param observer the observer to remove */ - public void consume() throws IOException { - final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE]; - while (read(buffer) != EOF) { - // empty - } + public void remove(final Observer observer) { + observers.remove(observer); + } + + /** + * Removes all Observers. + */ + public void removeAllObservers() { + observers.clear(); } }