Coverage Report - org.apache.commons.flatfile.util.ConcatenatedInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
ConcatenatedInputStream
90%
18/20
75%
9/12
0
ConcatenatedInputStream$1
100%
2/2
N/A
0
 
 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.flatfile.util;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.util.Iterator;
 22  
 
 23  
 import org.apache.commons.collections.IteratorUtils;
 24  
 
 25  
 /**
 26  
  * Unified InputStream representation of multiple concatenated InputStreams.
 27  
  * @version $Revision: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
 28  
  */
 29  
 public class ConcatenatedInputStream extends InputStream {
 30  
     /** EOF */
 31  
     public static final int EOF = -1;
 32  
 
 33  
     private static final String NO_NULL = "null arguments are unacceptable";
 34  1
     private static final InputStream AT_EOF = new InputStream() {
 35  1
         public int read() throws IOException {
 36  6
             return EOF;
 37  
         }
 38  
     };
 39  
 
 40  
     private Iterator<InputStream> iter;
 41  
     private InputStream current;
 42  
 
 43  
     /**
 44  
      * Create a new ConcatenatedInputStream.
 45  
      * @param src InputStreams
 46  
      */
 47  3
     public ConcatenatedInputStream(Iterable<InputStream> src) {
 48  3
         if (src == null) {
 49  0
             throw new IllegalArgumentException(NO_NULL);
 50  
         }
 51  3
         iter = src.iterator();
 52  3
         next();
 53  3
     }
 54  
 
 55  
     /**
 56  
      * Convenience constructor.
 57  
      * @param is0 first InputStream
 58  
      * @param is1 next InputStream
 59  
      */
 60  
     @SuppressWarnings("unchecked")
 61  18
     public ConcatenatedInputStream(InputStream is0, InputStream is1) {
 62  18
         if (is0 == null || is1 == null) {
 63  0
             throw new IllegalArgumentException(NO_NULL);
 64  
         }
 65  
         // shortcuts:
 66  18
         current = is0;
 67  18
         iter = IteratorUtils.singletonIterator(is1);
 68  18
     }
 69  
 
 70  
     /**
 71  
      * {@inheritDoc}
 72  
      */
 73  
     public int read() throws IOException {
 74  116
         int n = current.read();
 75  143
         while (n == EOF && current != AT_EOF) {
 76  27
             next();
 77  27
             n = current.read();
 78  
         }
 79  116
         return n;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Position to the next InputStream
 84  
      */
 85  
     private void next() {
 86  30
         current = iter.hasNext() ? iter.next() : AT_EOF;
 87  30
     }
 88  
 }