package innovimax.mixthem.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MultiChannelByteReader implements IMultiChannelByteInput {
	
	private final List<IByteInput> readers = new ArrayList<IByteInput>();
	
	/**
	* Constructor
	* @param inputs The list of inputs as InputResource
	* @see innovimax.mixthem.io.InputResource
	*/
	public MultiChannelByteReader(final List<InputResource> inputs) {	
		inputs.stream().forEach(input -> {
			try {
				this.readers.add(new DefaultByteReader(input));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		});
	}
	
	@Override
	public boolean hasByte() throws IOException {
		final Iterator<IByteInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final IByteInput reader = iterator.next();
			if (reader.hasByte()) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public byte[] nextByteRange() throws IOException {
		final byte[] bytes = new byte[this.readers.size()];
		int channel = 0;
		final Iterator<IByteInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final IByteInput reader = iterator.next();
			final byte b = reader.nextByte();
			bytes[channel++] = b;
		}
		return bytes;
	}
	
	@Override
	public void close() throws IOException {
		final Iterator<IByteInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final IByteInput reader = iterator.next();
			reader.close();
		}		
	}

}

@@ -351,16 +351,76 @@
 esource%0A
+%09* @param selection The input index selection (maybe empty)%0A
 %09* @see 
@@ -525,30 +525,218 @@
 puts
-) %7B%09%0A%09%09inputs.stream()
+, final Set%3CInteger%3E selection) %7B%09%0A%09%09IntStream.rangeClosed(1, inputs.size())%0A%09%09%09.filter(index -%3E selection.isEmpty() %7C%7C selection.contains(Integer.valueOf(index)))%0A%09%09%09.mapToObj(index -%3E inputs.get(index-1))%0A%09%09%09
 .for
@@ -751,16 +751,17 @@
 ut -%3E %7B%0A
+%09
 %09%09%09try %7B
@@ -765,16 +765,17 @@
 y %7B%0A%09%09%09%09
+%09
 this.rea
@@ -817,16 +817,17 @@
 t));%0A%09%09%09
+%09
 %7D catch 
@@ -840,24 +840,25 @@
 eption e) %7B%0A
+%09
 %09%09%09%09throw ne
@@ -883,18 +883,20 @@
 (e);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D);%0A%09%7D
@@ -962,45 +962,14 @@
 %7B%0A%09%09
-final Iterator%3CIByteInput%3E iterator =
+return
  thi
@@ -982,164 +982,161 @@
 ers.
-iterator
+stream
 ()
-;
 %0A%09%09
-while (iterator.hasNext()) %7B%0A%09%09%09final IByteInput reader = iterator.next();%0A%09%09%09if (reader.hasByte()) %7B%0A%09%09%09%09return true
+%09.anyMatch(reader -%3E %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09return reader.hasByte();%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09throw new RuntimeException(e)
 ;%0A
+%09
 %09%09%09%7D
-%0A
 %09%09
-%7D
 %0A%09%09
-return false
+%09%7D)
 ;%0A%09%7D
@@ -1562,170 +1562,144 @@
 %7B%0A%09%09
-final Iterator%3CIByteInput%3E iterator = this.readers.iterator();%0A%09%09while (iterator.hasNext()
+this.readers.forEach(reader -%3E %7B%0A%09%09%09try %7B%0A%09%09%09%09reader.close();%0A%09%09%09%7D catch (IOException e
 ) %7B%0A%09%09%09
+%09
-final IByteInput reader = iterator.next();%0A%09%09%09reader.close();
+throw new RuntimeException(e);%0A%09%09%09%7D
 %0A%09%09%7D
+);
 %09%09%0A%09

Update MultiChannelByteReader.java