28 import java.io.InputStream; |
28 import java.io.InputStream; |
29 import java.util.Enumeration; |
29 import java.util.Enumeration; |
30 import java.util.Vector; |
30 import java.util.Vector; |
31 |
31 |
32 /** |
32 /** |
33 * A <code>SequenceInputStream</code> represents |
33 * A {@code SequenceInputStream} represents |
34 * the logical concatenation of other input |
34 * the logical concatenation of other input |
35 * streams. It starts out with an ordered |
35 * streams. It starts out with an ordered |
36 * collection of input streams and reads from |
36 * collection of input streams and reads from |
37 * the first one until end of file is reached, |
37 * the first one until end of file is reached, |
38 * whereupon it reads from the second one, |
38 * whereupon it reads from the second one, |
46 class SequenceInputStream extends InputStream { |
46 class SequenceInputStream extends InputStream { |
47 Enumeration<? extends InputStream> e; |
47 Enumeration<? extends InputStream> e; |
48 InputStream in; |
48 InputStream in; |
49 |
49 |
50 /** |
50 /** |
51 * Initializes a newly created <code>SequenceInputStream</code> |
51 * Initializes a newly created {@code SequenceInputStream} |
52 * by remembering the argument, which must |
52 * by remembering the argument, which must |
53 * be an <code>Enumeration</code> that produces |
53 * be an {@code Enumeration} that produces |
54 * objects whose run-time type is <code>InputStream</code>. |
54 * objects whose run-time type is {@code InputStream}. |
55 * The input streams that are produced by |
55 * The input streams that are produced by |
56 * the enumeration will be read, in order, |
56 * the enumeration will be read, in order, |
57 * to provide the bytes to be read from this |
57 * to provide the bytes to be read from this |
58 * <code>SequenceInputStream</code>. After |
58 * {@code SequenceInputStream}. After |
59 * each input stream from the enumeration |
59 * each input stream from the enumeration |
60 * is exhausted, it is closed by calling its |
60 * is exhausted, it is closed by calling its |
61 * <code>close</code> method. |
61 * {@code close} method. |
62 * |
62 * |
63 * @param e an enumeration of input streams. |
63 * @param e an enumeration of input streams. |
64 * @see java.util.Enumeration |
64 * @see java.util.Enumeration |
65 */ |
65 */ |
66 public SequenceInputStream(Enumeration<? extends InputStream> e) { |
66 public SequenceInputStream(Enumeration<? extends InputStream> e) { |
68 peekNextStream(); |
68 peekNextStream(); |
69 } |
69 } |
70 |
70 |
71 /** |
71 /** |
72 * Initializes a newly |
72 * Initializes a newly |
73 * created <code>SequenceInputStream</code> |
73 * created {@code SequenceInputStream} |
74 * by remembering the two arguments, which |
74 * by remembering the two arguments, which |
75 * will be read in order, first <code>s1</code> |
75 * will be read in order, first {@code s1} |
76 * and then <code>s2</code>, to provide the |
76 * and then {@code s2}, to provide the |
77 * bytes to be read from this <code>SequenceInputStream</code>. |
77 * bytes to be read from this {@code SequenceInputStream}. |
78 * |
78 * |
79 * @param s1 the first input stream to read. |
79 * @param s1 the first input stream to read. |
80 * @param s2 the second input stream to read. |
80 * @param s2 the second input stream to read. |
81 */ |
81 */ |
82 public SequenceInputStream(InputStream s1, InputStream s2) { |
82 public SequenceInputStream(InputStream s1, InputStream s2) { |
133 return in.available(); |
133 return in.available(); |
134 } |
134 } |
135 |
135 |
136 /** |
136 /** |
137 * Reads the next byte of data from this input stream. The byte is |
137 * Reads the next byte of data from this input stream. The byte is |
138 * returned as an <code>int</code> in the range <code>0</code> to |
138 * returned as an {@code int} in the range {@code 0} to |
139 * <code>255</code>. If no byte is available because the end of the |
139 * {@code 255}. If no byte is available because the end of the |
140 * stream has been reached, the value <code>-1</code> is returned. |
140 * stream has been reached, the value {@code -1} is returned. |
141 * This method blocks until input data is available, the end of the |
141 * This method blocks until input data is available, the end of the |
142 * stream is detected, or an exception is thrown. |
142 * stream is detected, or an exception is thrown. |
143 * <p> |
143 * <p> |
144 * This method |
144 * This method |
145 * tries to read one character from the current substream. If it |
145 * tries to read one character from the current substream. If it |
146 * reaches the end of the stream, it calls the <code>close</code> |
146 * reaches the end of the stream, it calls the {@code close} |
147 * method of the current substream and begins reading from the next |
147 * method of the current substream and begins reading from the next |
148 * substream. |
148 * substream. |
149 * |
149 * |
150 * @return the next byte of data, or <code>-1</code> if the end of the |
150 * @return the next byte of data, or {@code -1} if the end of the |
151 * stream is reached. |
151 * stream is reached. |
152 * @throws IOException if an I/O error occurs. |
152 * @throws IOException if an I/O error occurs. |
153 */ |
153 */ |
154 public int read() throws IOException { |
154 public int read() throws IOException { |
155 while (in != null) { |
155 while (in != null) { |
161 } |
161 } |
162 return -1; |
162 return -1; |
163 } |
163 } |
164 |
164 |
165 /** |
165 /** |
166 * Reads up to <code>len</code> bytes of data from this input stream |
166 * Reads up to {@code len} bytes of data from this input stream |
167 * into an array of bytes. If <code>len</code> is not zero, the method |
167 * into an array of bytes. If {@code len} is not zero, the method |
168 * blocks until at least 1 byte of input is available; otherwise, no |
168 * blocks until at least 1 byte of input is available; otherwise, no |
169 * bytes are read and <code>0</code> is returned. |
169 * bytes are read and {@code 0} is returned. |
170 * <p> |
170 * <p> |
171 * The <code>read</code> method of <code>SequenceInputStream</code> |
171 * The {@code read} method of {@code SequenceInputStream} |
172 * tries to read the data from the current substream. If it fails to |
172 * tries to read the data from the current substream. If it fails to |
173 * read any characters because the substream has reached the end of |
173 * read any characters because the substream has reached the end of |
174 * the stream, it calls the <code>close</code> method of the current |
174 * the stream, it calls the {@code close} method of the current |
175 * substream and begins reading from the next substream. |
175 * substream and begins reading from the next substream. |
176 * |
176 * |
177 * @param b the buffer into which the data is read. |
177 * @param b the buffer into which the data is read. |
178 * @param off the start offset in array <code>b</code> |
178 * @param off the start offset in array {@code b} |
179 * at which the data is written. |
179 * at which the data is written. |
180 * @param len the maximum number of bytes read. |
180 * @param len the maximum number of bytes read. |
181 * @return int the number of bytes read. |
181 * @return int the number of bytes read. |
182 * @throws NullPointerException If <code>b</code> is <code>null</code>. |
182 * @throws NullPointerException If {@code b} is {@code null}. |
183 * @throws IndexOutOfBoundsException If <code>off</code> is negative, |
183 * @throws IndexOutOfBoundsException If {@code off} is negative, |
184 * <code>len</code> is negative, or <code>len</code> is |
184 * {@code len} is negative, or {@code len} is |
185 * greater than <code>b.length - off</code> |
185 * greater than {@code b.length - off} |
186 * @throws IOException if an I/O error occurs. |
186 * @throws IOException if an I/O error occurs. |
187 */ |
187 */ |
188 public int read(byte b[], int off, int len) throws IOException { |
188 public int read(byte b[], int off, int len) throws IOException { |
189 if (in == null) { |
189 if (in == null) { |
190 return -1; |
190 return -1; |
206 } |
206 } |
207 |
207 |
208 /** |
208 /** |
209 * Closes this input stream and releases any system resources |
209 * Closes this input stream and releases any system resources |
210 * associated with the stream. |
210 * associated with the stream. |
211 * A closed <code>SequenceInputStream</code> |
211 * A closed {@code SequenceInputStream} |
212 * cannot perform input operations and cannot |
212 * cannot perform input operations and cannot |
213 * be reopened. |
213 * be reopened. |
214 * <p> |
214 * <p> |
215 * If this stream was created |
215 * If this stream was created |
216 * from an enumeration, all remaining elements |
216 * from an enumeration, all remaining elements |
217 * are requested from the enumeration and closed |
217 * are requested from the enumeration and closed |
218 * before the <code>close</code> method returns. |
218 * before the {@code close} method returns. |
219 * |
219 * |
220 * @throws IOException if an I/O error occurs. |
220 * @throws IOException if an I/O error occurs. |
221 */ |
221 */ |
222 public void close() throws IOException { |
222 public void close() throws IOException { |
223 IOException ioe = null; |
223 IOException ioe = null; |