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
	* @param selection The input index selection (maybe empty)
	* @see innovimax.mixthem.io.InputResource
	*/
	public MultiChannelByteReader(final List<InputResource> inputs, final Set<Integer> selection) {
		try {
			IntStream.range(0, inputs.size())
				.filter(index -> selection.contains(Integer.valueOf(index))
				.mapToObj(index -> inputs.get(index))
				.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();
		}		
	}

}

@@ -558,18 +558,11 @@
 n) %7B
-%0A
 %09%09
-try %7B%0A%09
+%0A
 %09%09In
@@ -593,17 +593,16 @@
 size())%0A
-%09
 %09%09%09.filt
@@ -659,17 +659,16 @@
 ex))%0A%09%09%09
-%09
 .mapToOb
@@ -697,17 +697,16 @@
 index))%0A
-%09
 %09%09%09.forE
@@ -728,16 +728,14 @@
 %09%09%09%09
-%09
 try %7B%0A
-%09
 %09%09%09%09
@@ -783,17 +783,16 @@
 nput));%0A
-%09
 %09%09%09%09%7D ca
@@ -818,17 +818,16 @@
  %7B%0A%09%09%09%09%09
-%09
 throw ne
@@ -849,17 +849,16 @@
 ion(e);%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -862,13 +862,16 @@
 %0A%09%09%09
-%09
 %7D);%0A
+%09%09%09%0A
 %09%7D%0A%09

Update MultiChannelByteReader.java