|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.io.InputStream; |
21 | | -import java.security.MessageDigest; |
22 | 21 | import java.util.ArrayList; |
23 | 22 | import java.util.List; |
24 | 23 |
|
|
36 | 35 | * @see MessageDigestCalculatingInputStream |
37 | 36 | */ |
38 | 37 | public class ObservableInputStream extends ProxyInputStream { |
| 38 | + |
39 | 39 | public static abstract class Observer { |
| 40 | + |
40 | 41 | /** Called to indicate, that {@link InputStream#read()} has been invoked |
41 | 42 | * on the {@link ObservableInputStream}, and will return a value. |
42 | 43 | * @param pByte The value, which is being returned. This will never be -1 (EOF), |
43 | 44 | * because, in that case, {@link #finished()} will be invoked instead. |
| 45 | + * @throws IOException if an i/o-error occurs |
44 | 46 | */ |
45 | 47 | void data(int pByte) throws IOException {} |
| 48 | + |
46 | 49 | /** Called to indicate, that {@link InputStream#read(byte[])}, or |
47 | 50 | * {@link InputStream#read(byte[], int, int)} have been called, and are about to |
48 | 51 | * invoke data. |
49 | 52 | * @param pBuffer The byte array, which has been passed to the read call, and where |
50 | 53 | * data has been stored. |
51 | 54 | * @param pOffset The offset within the byte array, where data has been stored. |
52 | 55 | * @param pLength The number of bytes, which have been stored in the byte array. |
| 56 | + * @throws IOException if an i/o-error occurs |
53 | 57 | */ |
54 | 58 | void data(byte[] pBuffer, int pOffset, int pLength) throws IOException {} |
| 59 | + |
55 | 60 | /** Called to indicate, that EOF has been seen on the underlying stream. |
56 | 61 | * This method may be called multiple times, if the reader keeps invoking |
57 | 62 | * either of the read methods, and they will consequently keep returning |
58 | 63 | * EOF. |
| 64 | + * @throws IOException if an i/o-error occurs |
59 | 65 | */ |
60 | 66 | void finished() throws IOException {} |
| 67 | + |
61 | 68 | /** Called to indicate, that the {@link ObservableInputStream} has been closed. |
| 69 | + * @throws IOException if an i/o-error occurs |
62 | 70 | */ |
63 | 71 | void closed() throws IOException {} |
| 72 | + |
64 | 73 | /** |
65 | 74 | * Called to indicate, that an error occurred on the underlying stream. |
| 75 | + * @throws IOException if an i/o-error occurs |
66 | 76 | */ |
67 | 77 | void error(IOException pException) throws IOException { throw pException; } |
68 | 78 | } |
69 | 79 |
|
70 | 80 | private final List<Observer> observers = new ArrayList<>(); |
71 | | - |
| 81 | + |
| 82 | + /** |
| 83 | + * Creates a new ObservableInputStream for the given InputStream. |
| 84 | + * @param pProxy the input stream to proxy |
| 85 | + */ |
72 | 86 | public ObservableInputStream(InputStream pProxy) { |
73 | 87 | super(pProxy); |
74 | 88 | } |
75 | 89 |
|
| 90 | + /** |
| 91 | + * Adds an Observer. |
| 92 | + * @param pObserver the observer to add |
| 93 | + */ |
76 | 94 | public void add(Observer pObserver) { |
77 | 95 | observers.add(pObserver); |
78 | 96 | } |
79 | 97 |
|
| 98 | + /** |
| 99 | + * Removes an Observer. |
| 100 | + * @param pObserver the observer to remove |
| 101 | + */ |
80 | 102 | public void remove(Observer pObserver) { |
81 | 103 | observers.remove(pObserver); |
82 | 104 | } |
83 | 105 |
|
| 106 | + /** |
| 107 | + * Removes all Observers. |
| 108 | + */ |
84 | 109 | public void removeAllObservers() { |
85 | 110 | observers.clear(); |
86 | 111 | } |
@@ -201,6 +226,9 @@ protected void noteClosed() throws IOException { |
201 | 226 | } |
202 | 227 | } |
203 | 228 |
|
| 229 | + /** Gets all currently registered observers. |
| 230 | + * @return a list of the currently registered observers |
| 231 | + */ |
204 | 232 | protected List<Observer> getObservers() { |
205 | 233 | return observers; |
206 | 234 | } |
|
0 commit comments