Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
CompositeReader | 31 | 10 | 0% | 5 | 0 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.extension.job.history.internal; | |
21 | ||
22 | import java.io.IOException; | |
23 | import java.io.Reader; | |
24 | ||
25 | /** | |
26 | * A composite {@link Reader} (because there is {@code SequenceInputStream} but not {@code SequenceReader}). | |
27 | * | |
28 | * @version $Id: b5ca37d8c4832713bcef3d648bb6aae2455058d3 $ | |
29 | * @since 7.1RC1 | |
30 | */ | |
31 | public class CompositeReader extends Reader | |
32 | { | |
33 | private final Reader[] readers; | |
34 | ||
35 | private int index; | |
36 | ||
37 | /** | |
38 | * Creates a new composite reader that includes the given readers. | |
39 | * | |
40 | * @param readers the readers to include | |
41 | */ | |
42 | 2 | public CompositeReader(Reader... readers) |
43 | { | |
44 | 2 | this.readers = readers; |
45 | } | |
46 | ||
47 | 1 | @Override |
48 | public void close() throws IOException | |
49 | { | |
50 | 1 | for (Reader reader : this.readers) { |
51 | 2 | reader.close(); |
52 | } | |
53 | } | |
54 | ||
55 | 5 | @Override |
56 | public int read(char[] buffer, int offset, int length) throws IOException | |
57 | { | |
58 | 5 | if (this.index >= this.readers.length) { |
59 | // No more readers. | |
60 | 1 | return -1; |
61 | } | |
62 | ||
63 | 4 | int readCount = this.readers[this.index].read(buffer, offset, length); |
64 | 4 | if (readCount < 0) { |
65 | // End of the current reader. Move to the next reader. | |
66 | 2 | this.index++; |
67 | 2 | return read(buffer, offset, length); |
68 | } | |
69 | ||
70 | 2 | return readCount; |
71 | } | |
72 | } |