Internal refactroring. Javadoc. Sort members.
MODIFY
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
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 void data(final byte[] buffer, final int offset, final int length) throws
}
/**
- * 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 void add(final Observer observer) {
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 @@ protected void noteDataBytes(final byte[] buffer, final int offset, final int le
*
* @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();
}
}
@@ -230,6 +179,20 @@ protected void noteDataByte(final int value) throws IOException {
}
}
+ /**
+ * 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.
*
@@ -248,46 +211,81 @@ protected void noteError(final IOException exception) throws IOException {
*
* @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();
}
}

Internal refactroring. Javadoc. Sort members.