@@ -42,15 +42,12 @@ public class ObservableInputStream extends ProxyInputStream {
4242 public static abstract class Observer {
4343
4444 /**
45- * Called to indicate, that {@link InputStream#read()} has been invoked on the {@link ObservableInputStream},
46- * and will return a value.
45+ * Called to indicate that the {@link ObservableInputStream} has been closed.
4746 *
48- * @param value The value, which is being returned. This will never be -1 (EOF), because, in that case,
49- * {@link #finished()} will be invoked instead.
5047 * @throws IOException if an I/O error occurs.
5148 */
5249 @ SuppressWarnings ("unused" ) // Possibly thrown from subclasses.
53- public void data ( final int value ) throws IOException {
50+ public void closed ( ) throws IOException {
5451 // noop
5552 }
5653
@@ -69,34 +66,37 @@ public void data(final byte[] buffer, final int offset, final int length) throws
6966 }
7067
7168 /**
72- * Called to indicate that EOF has been seen on the underlying stream. This method may be called multiple times ,
73- * if the reader keeps invoking either of the read methods, and they will consequently keep returning EOF .
69+ * Called to indicate, that {@link InputStream#read()} has been invoked on the {@link ObservableInputStream} ,
70+ * and will return a value .
7471 *
72+ * @param value The value, which is being returned. This will never be -1 (EOF), because, in that case,
73+ * {@link #finished()} will be invoked instead.
7574 * @throws IOException if an I/O error occurs.
7675 */
7776 @ SuppressWarnings ("unused" ) // Possibly thrown from subclasses.
78- public void finished ( ) throws IOException {
77+ public void data ( final int value ) throws IOException {
7978 // noop
8079 }
8180
8281 /**
83- * Called to indicate that the {@link ObservableInputStream} has been closed .
82+ * Called to indicate that an error occurred on the underlying stream .
8483 *
84+ * @param exception the exception to throw
8585 * @throws IOException if an I/O error occurs.
8686 */
87- @ SuppressWarnings ("unused" ) // Possibly thrown from subclasses.
88- public void closed () throws IOException {
89- // noop
87+ public void error (final IOException exception ) throws IOException {
88+ throw exception ;
9089 }
9190
9291 /**
93- * Called to indicate that an error occurred on the underlying stream.
92+ * Called to indicate that EOF has been seen on the underlying stream. This method may be called multiple times,
93+ * if the reader keeps invoking either of the read methods, and they will consequently keep returning EOF.
9494 *
95- * @param exception the exception to throw
9695 * @throws IOException if an I/O error occurs.
9796 */
98- public void error (final IOException exception ) throws IOException {
99- throw exception ;
97+ @ SuppressWarnings ("unused" ) // Possibly thrown from subclasses.
98+ public void finished () throws IOException {
99+ // noop
100100 }
101101 }
102102
@@ -120,101 +120,50 @@ public void add(final Observer observer) {
120120 observers .add (observer );
121121 }
122122
123- /**
124- * Removes an Observer.
125- *
126- * @param observer the observer to remove
127- */
128- public void remove (final Observer observer ) {
129- observers .remove (observer );
130- }
131-
132- /**
133- * Removes all Observers.
134- */
135- public void removeAllObservers () {
136- observers .clear ();
137- }
138-
139123 @ Override
140- public int read () throws IOException {
141- int result = 0 ;
124+ public void close () throws IOException {
142125 IOException ioe = null ;
143126 try {
144- result = super .read ();
145- } catch (final IOException pException ) {
146- ioe = pException ;
127+ super .close ();
128+ } catch (final IOException e ) {
129+ ioe = e ;
147130 }
148- if (ioe != null ) {
149- noteError (ioe );
150- } else if (result == EOF ) {
151- noteFinished ();
131+ if (ioe == null ) {
132+ noteClosed ();
152133 } else {
153- noteDataByte (result );
154- }
155- return result ;
156- }
157-
158- @ Override
159- public int read (final byte [] buffer ) throws IOException {
160- int result = 0 ;
161- IOException ioe = null ;
162- try {
163- result = super .read (buffer );
164- } catch (final IOException pException ) {
165- ioe = pException ;
166- }
167- if (ioe != null ) {
168134 noteError (ioe );
169- } else if (result == EOF ) {
170- noteFinished ();
171- } else if (result > 0 ) {
172- noteDataBytes (buffer , 0 , result );
173135 }
174- return result ;
175136 }
176137
177- @ Override
178- public int read (final byte [] buffer , final int offset , final int length ) throws IOException {
179- int result = 0 ;
180- IOException ioe = null ;
181- try {
182- result = super .read (buffer , offset , length );
183- } catch (final IOException pException ) {
184- ioe = pException ;
185- }
186- if (ioe != null ) {
187- noteError (ioe );
188- } else if (result == EOF ) {
189- noteFinished ();
190- } else if (result > 0 ) {
191- noteDataBytes (buffer , offset , result );
138+ /**
139+ * Reads all data from the underlying {@link InputStream}, while notifying the observers.
140+ *
141+ * @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception.
142+ */
143+ public void consume () throws IOException {
144+ final byte [] buffer = new byte [IOUtils .DEFAULT_BUFFER_SIZE ];
145+ while (read (buffer ) != EOF ) {
146+ // empty
192147 }
193- return result ;
194148 }
195149
196150 /**
197- * Notifies the observers by invoking {@link Observer#data(byte[],int,int)} with the given arguments .
151+ * Gets all currently registered observers .
198152 *
199- * @param buffer Passed to the observers.
200- * @param offset Passed to the observers.
201- * @param length Passed to the observers.
202- * @throws IOException Some observer has thrown an exception, which is being passed down.
153+ * @return a list of the currently registered observers
203154 */
204- protected void noteDataBytes (final byte [] buffer , final int offset , final int length ) throws IOException {
205- for (final Observer observer : getObservers ()) {
206- observer .data (buffer , offset , length );
207- }
155+ protected List <Observer > getObservers () {
156+ return observers ;
208157 }
209158
210159 /**
211160 * Notifies the observers by invoking {@link Observer#finished()}.
212161 *
213162 * @throws IOException Some observer has thrown an exception, which is being passed down.
214163 */
215- protected void noteFinished () throws IOException {
164+ protected void noteClosed () throws IOException {
216165 for (final Observer observer : getObservers ()) {
217- observer .finished ();
166+ observer .closed ();
218167 }
219168 }
220169
@@ -230,6 +179,20 @@ protected void noteDataByte(final int value) throws IOException {
230179 }
231180 }
232181
182+ /**
183+ * Notifies the observers by invoking {@link Observer#data(byte[],int,int)} with the given arguments.
184+ *
185+ * @param buffer Passed to the observers.
186+ * @param offset Passed to the observers.
187+ * @param length Passed to the observers.
188+ * @throws IOException Some observer has thrown an exception, which is being passed down.
189+ */
190+ protected void noteDataBytes (final byte [] buffer , final int offset , final int length ) throws IOException {
191+ for (final Observer observer : getObservers ()) {
192+ observer .data (buffer , offset , length );
193+ }
194+ }
195+
233196 /**
234197 * Notifies the observers by invoking {@link Observer#error(IOException)} with the given argument.
235198 *
@@ -248,46 +211,81 @@ protected void noteError(final IOException exception) throws IOException {
248211 *
249212 * @throws IOException Some observer has thrown an exception, which is being passed down.
250213 */
251- protected void noteClosed () throws IOException {
214+ protected void noteFinished () throws IOException {
252215 for (final Observer observer : getObservers ()) {
253- observer .closed ();
216+ observer .finished ();
254217 }
255218 }
256219
257- /**
258- * Gets all currently registered observers.
259- *
260- * @return a list of the currently registered observers
261- */
262- protected List <Observer > getObservers () {
263- return observers ;
220+ private void notify (final byte [] buffer , final int offset , int result , IOException ioe ) throws IOException {
221+ if (ioe != null ) {
222+ noteError (ioe );
223+ } else if (result == EOF ) {
224+ noteFinished ();
225+ } else if (result > 0 ) {
226+ noteDataBytes (buffer , offset , result );
227+ }
264228 }
265229
266230 @ Override
267- public void close () throws IOException {
231+ public int read () throws IOException {
232+ int result = 0 ;
268233 IOException ioe = null ;
269234 try {
270- super .close ();
271- } catch (final IOException e ) {
272- ioe = e ;
235+ result = super .read ();
236+ } catch (final IOException pException ) {
237+ ioe = pException ;
273238 }
274- if (ioe == null ) {
275- noteClosed ();
276- } else {
239+ if (ioe != null ) {
277240 noteError (ioe );
241+ } else if (result == EOF ) {
242+ noteFinished ();
243+ } else {
244+ noteDataByte (result );
245+ }
246+ return result ;
247+ }
248+
249+ @ Override
250+ public int read (final byte [] buffer ) throws IOException {
251+ int result = 0 ;
252+ IOException ioe = null ;
253+ try {
254+ result = super .read (buffer );
255+ } catch (final IOException pException ) {
256+ ioe = pException ;
257+ }
258+ notify (buffer , 0 , result , ioe );
259+ return result ;
260+ }
261+
262+ @ Override
263+ public int read (final byte [] buffer , final int offset , final int length ) throws IOException {
264+ int result = 0 ;
265+ IOException ioe = null ;
266+ try {
267+ result = super .read (buffer , offset , length );
268+ } catch (final IOException pException ) {
269+ ioe = pException ;
278270 }
271+ notify (buffer , offset , result , ioe );
272+ return result ;
279273 }
280274
281275 /**
282- * Reads all data from the underlying {@link InputStream}, while notifying the observers .
276+ * Removes an Observer .
283277 *
284- * @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception.
278+ * @param observer the observer to remove
285279 */
286- public void consume () throws IOException {
287- final byte [] buffer = new byte [IOUtils .DEFAULT_BUFFER_SIZE ];
288- while (read (buffer ) != EOF ) {
289- // empty
290- }
280+ public void remove (final Observer observer ) {
281+ observers .remove (observer );
282+ }
283+
284+ /**
285+ * Removes all Observers.
286+ */
287+ public void removeAllObservers () {
288+ observers .clear ();
291289 }
292290
293291}
0 commit comments