| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.apache.commons.io.input; |
| 18 |
|
| 19 |
import static org.apache.commons.io.IOUtils.EOF; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.io.Reader; |
| 23 |
import java.io.SequenceInputStream; |
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.Objects; |
| 27 |
|
| 28 |
import org.apache.commons.io.function.Uncheck; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class SequenceReader extends Reader { |
| 39 |
|
| 40 |
private Reader reader; |
| 41 |
private final Iterator<? extends Reader> readers; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public SequenceReader(final Iterable<? extends Reader> readers) { |
| 49 |
this.readers = Objects.requireNonNull(readers, "readers").iterator(); |
| 50 |
this.reader = Uncheck.get(this::nextReader); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public SequenceReader(final Reader... readers) { |
| 59 |
this(Arrays.asList(readers)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
@Override |
| 68 |
public void close() throws IOException { |
| 69 |
do { |
| 70 |
|
| 71 |
} while (nextReader() != null); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private Reader nextReader() throws IOException { |
| 81 |
if (reader != null) { |
| 82 |
reader.close(); |
| 83 |
} |
| 84 |
if (readers.hasNext()) { |
| 85 |
reader = readers.next(); |
| 86 |
} else { |
| 87 |
reader = null; |
| 88 |
} |
| 89 |
return reader; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
@Override |
| 98 |
public int read() throws IOException { |
| 99 |
int c = EOF; |
| 100 |
while (reader != null) { |
| 101 |
c = reader.read(); |
| 102 |
if (c != EOF) { |
| 103 |
break; |
| 104 |
} |
| 105 |
nextReader(); |
| 106 |
} |
| 107 |
return c; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@Override |
| 116 |
public int read(final char[] cbuf, int off, int len) throws IOException { |
| 117 |
Objects.requireNonNull(cbuf, "cbuf"); |
| 118 |
if (len < 0 || off < 0 || off + len > cbuf.length) { |
| 119 |
throw new IndexOutOfBoundsException("Array Size=" + cbuf.length + ", offset=" + off + ", length=" + len); |
| 120 |
} |
| 121 |
int count = 0; |
| 122 |
while (reader != null) { |
| 123 |
final int readLen = reader.read(cbuf, off, len); |
| 124 |
if (readLen == EOF) { |
| 125 |
nextReader(); |
| 126 |
} else { |
| 127 |
count += readLen; |
| 128 |
off += readLen; |
| 129 |
len -= readLen; |
| 130 |
if (len <= 0) { |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
if (count > 0) { |
| 136 |
return count; |
| 137 |
} |
| 138 |
return EOF; |
| 139 |
} |
| 140 |
} |