View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Provides the contents of multiple {@link Reader}s in sequence.
32   * <p>
33   * Like {@link SequenceInputStream} but for {@link Reader} arguments.
34   * </p>
35   *
36   * @since 2.7
37   */
38  public class SequenceReader extends Reader {
39  
40      private Reader reader;
41      private final Iterator<? extends Reader> readers;
42  
43      /**
44       * Constructs a new instance with readers
45       *
46       * @param readers the readers to read
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       * Constructs a new instance with readers
55       *
56       * @param readers the readers to read
57       */
58      public SequenceReader(final Reader... readers) {
59          this(Arrays.asList(readers));
60      }
61  
62      /*
63       * (non-Javadoc)
64       *
65       * @see java.io.Reader#close()
66       */
67      @Override
68      public void close() throws IOException {
69          do { // NOPMD
70               // empty
71          } while (nextReader() != null);
72      }
73  
74      /**
75       * Returns the next available reader or null if done.
76       *
77       * @return the next available reader or null.
78       * @throws IOException IOException  If an I/O error occurs.
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       * (non-Javadoc)
94       *
95       * @see java.io.Reader#read(char[], int, int)
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      * (non-Javadoc)
112      *
113      * @see java.io.Reader#read()
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 }