src/core/net/sf/basedb/util/InputStreamCombiner.java

Code
Comments
Other
Rev Date Author Line
5353 27 May 10 nicklas 1 /**
5353 27 May 10 nicklas 2   $Id$
5353 27 May 10 nicklas 3
5353 27 May 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5353 27 May 10 nicklas 5
5353 27 May 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5353 27 May 10 nicklas 7   Available at http://base.thep.lu.se/
5353 27 May 10 nicklas 8
5353 27 May 10 nicklas 9   BASE is free software; you can redistribute it and/or
5353 27 May 10 nicklas 10   modify it under the terms of the GNU General Public License
5353 27 May 10 nicklas 11   as published by the Free Software Foundation; either version 3
5353 27 May 10 nicklas 12   of the License, or (at your option) any later version.
5353 27 May 10 nicklas 13
5353 27 May 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5353 27 May 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5353 27 May 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5353 27 May 10 nicklas 17   GNU General Public License for more details.
5353 27 May 10 nicklas 18
5353 27 May 10 nicklas 19   You should have received a copy of the GNU General Public License
5353 27 May 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5353 27 May 10 nicklas 21 */
5353 27 May 10 nicklas 22 package net.sf.basedb.util;
5353 27 May 10 nicklas 23
5353 27 May 10 nicklas 24 import java.io.IOException;
5353 27 May 10 nicklas 25 import java.io.InputStream;
5353 27 May 10 nicklas 26
5353 27 May 10 nicklas 27 /**
5353 27 May 10 nicklas 28   An input stream implementation that reads one or more input streams
5353 27 May 10 nicklas 29   sequentially. When the end of the first stream has been reached, reading
5353 27 May 10 nicklas 30   continues on the next one, and so on until all streams have been read.
5353 27 May 10 nicklas 31   The underlying streams are automatically closed once they have been fully 
5353 27 May 10 nicklas 32   read.
5353 27 May 10 nicklas 33
5353 27 May 10 nicklas 34   @author Nicklas
5353 27 May 10 nicklas 35   @since 2.16
5353 27 May 10 nicklas 36   @base.modified $Date$
5353 27 May 10 nicklas 37 */
5353 27 May 10 nicklas 38 public class InputStreamCombiner
5353 27 May 10 nicklas 39   extends InputStream
5353 27 May 10 nicklas 40 {
5353 27 May 10 nicklas 41
5353 27 May 10 nicklas 42   private final InputStream[] in;
5353 27 May 10 nicklas 43   private int index;
5353 27 May 10 nicklas 44   
5353 27 May 10 nicklas 45   /**
5353 27 May 10 nicklas 46     Create a new stream combiner using the given streams to read from
5353 27 May 10 nicklas 47     @param in The stream to read from, must be an array with at least
5353 27 May 10 nicklas 48       one stream
5353 27 May 10 nicklas 49   */
5353 27 May 10 nicklas 50   public InputStreamCombiner(InputStream... in)
5353 27 May 10 nicklas 51   {
5353 27 May 10 nicklas 52     this.in = in;
5353 27 May 10 nicklas 53     if (in == null || in.length == 0) throw new NullPointerException("'in' is null or empty");
5353 27 May 10 nicklas 54     this.index = 0;
5353 27 May 10 nicklas 55   }
5353 27 May 10 nicklas 56   
5353 27 May 10 nicklas 57   /*
5353 27 May 10 nicklas 58     From the InputStream class
5353 27 May 10 nicklas 59     --------------------------
5353 27 May 10 nicklas 60   */
5353 27 May 10 nicklas 61   @Override
5353 27 May 10 nicklas 62   public int available() 
5353 27 May 10 nicklas 63     throws IOException
5353 27 May 10 nicklas 64   {
5353 27 May 10 nicklas 65     if (index >= in.length) return 0;
5353 27 May 10 nicklas 66     return in[index].available();
5353 27 May 10 nicklas 67   }
5353 27 May 10 nicklas 68
5353 27 May 10 nicklas 69   /**
5353 27 May 10 nicklas 70     Closes all underlying streams.
5353 27 May 10 nicklas 71   */
5353 27 May 10 nicklas 72   @Override
5353 27 May 10 nicklas 73   public void close() 
5353 27 May 10 nicklas 74     throws IOException
5353 27 May 10 nicklas 75   {
5353 27 May 10 nicklas 76     while (index < in.length)
5353 27 May 10 nicklas 77     {
5353 27 May 10 nicklas 78       nextStream();
5353 27 May 10 nicklas 79     }
5353 27 May 10 nicklas 80   }
5353 27 May 10 nicklas 81
5353 27 May 10 nicklas 82   @Override
5353 27 May 10 nicklas 83   public int read(byte[] b, int off, int len) 
5353 27 May 10 nicklas 84     throws IOException
5353 27 May 10 nicklas 85   {
5353 27 May 10 nicklas 86     int result = -1;
5353 27 May 10 nicklas 87     while (result == -1 && index < in.length)
5353 27 May 10 nicklas 88     {
5353 27 May 10 nicklas 89       result = in[index].read(b, off, len);
5353 27 May 10 nicklas 90       if (result == -1) nextStream();
5353 27 May 10 nicklas 91     }
5353 27 May 10 nicklas 92     return result;    
5353 27 May 10 nicklas 93   }
5353 27 May 10 nicklas 94
5353 27 May 10 nicklas 95   @Override
5353 27 May 10 nicklas 96   public int read(byte[] b) 
5353 27 May 10 nicklas 97     throws IOException
5353 27 May 10 nicklas 98   {
5353 27 May 10 nicklas 99     return read(b, 0, b.length);
5353 27 May 10 nicklas 100   }
5353 27 May 10 nicklas 101
5353 27 May 10 nicklas 102   @Override
5353 27 May 10 nicklas 103   public long skip(long n) 
5353 27 May 10 nicklas 104     throws IOException
5353 27 May 10 nicklas 105   {
5353 27 May 10 nicklas 106     if (index >= in.length) return 0;
5353 27 May 10 nicklas 107     long skipped = 0;
5353 27 May 10 nicklas 108     while (read() != -1 && skipped < n)
5353 27 May 10 nicklas 109     {
5353 27 May 10 nicklas 110       ++skipped;
5353 27 May 10 nicklas 111     }
5353 27 May 10 nicklas 112     return skipped;
5353 27 May 10 nicklas 113   }
5353 27 May 10 nicklas 114
5353 27 May 10 nicklas 115   @Override
5353 27 May 10 nicklas 116   public int read() 
5353 27 May 10 nicklas 117     throws IOException
5353 27 May 10 nicklas 118   {
5353 27 May 10 nicklas 119     int result = -1;
5353 27 May 10 nicklas 120     while (result == -1 && index < in.length)
5353 27 May 10 nicklas 121     {
5353 27 May 10 nicklas 122       result = in[index].read();
5353 27 May 10 nicklas 123       if (result == -1) nextStream();
5353 27 May 10 nicklas 124     }
5353 27 May 10 nicklas 125     return result;
5353 27 May 10 nicklas 126   }
5353 27 May 10 nicklas 127   // -------------------------------------
5353 27 May 10 nicklas 128
5353 27 May 10 nicklas 129   private void nextStream()
5353 27 May 10 nicklas 130   {
5353 27 May 10 nicklas 131     FileUtil.close(in[index]);
5353 27 May 10 nicklas 132     ++index;
5353 27 May 10 nicklas 133   }
5353 27 May 10 nicklas 134   
5353 27 May 10 nicklas 135 }