package innovimax.mixthem.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;

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) {	
		IntStream.rangeClosed(1, inputs.size())
			.filter(index -> selection.isEmpty() || selection.contains(Integer.valueOf(index)))
			.mapToObj(index -> inputs.get(index-1))
			.forEachOrdered(input -> {
				try {
					this.readers.add(new DefaultByteReader(input));
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			});
	}
	
	@Override
	public boolean hasByte() throws IOException {
		return this.readers.stream()
			.anyMatch(reader -> {
				try {
					return reader.hasByte();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}		
			});
	}
	
	@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 {
		this.readers.forEach(reader -> {
			try {
				reader.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		});		
	}

}

@@ -43,74 +43,57 @@
 .io.
-IOException;%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator
+ByteArrayOutputStream;%0Aimport java.io.IOException
 ;%0Aim
@@ -288,38 +288,8 @@
 ders
- = new ArrayList%3CIByteInput%3E()
 ;%0A%09%0A
@@ -568,16 +568,31 @@
 n) %7B%09%0A%09%09
+this.readers = 
 IntStrea
@@ -761,22 +761,11 @@
 %09%09%09.
-forEachOrdered
+map
 (inp
@@ -791,25 +791,15 @@
 %09%09%09%09
-this.readers.add(
+return 
 new 
@@ -805,20 +805,20 @@
  Default
-Byte
+Char
 Reader(i
@@ -822,17 +822,16 @@
 r(input)
-)
 ;%0A%09%09%09%09%7D 
@@ -895,21 +895,50 @@
 );%0A%09%09%09%09%7D
+%7D)
 %0A%09%09%09
-%7D
+.collect(Collectors.toList()
 );%0A%09%7D%0A%09%0A
@@ -1166,14 +1166,8 @@
 %09%09%09%7D
-%09%09%0A%09%09%09
 %7D);%0A
@@ -1236,16 +1236,18 @@
 ion %7B%0A%09%09
+/*
 final by
@@ -1532,16 +1532,385 @@
 n bytes;
+*/%0A%09%09return readers.stream()%0A%09%09%09.map(reader -%3E %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09return Byte.valueOf(reader.nextByte());%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%09%0A%09%09%09%7D)%09%09%09%0A%09%09%09.collect(ByteArrayOutputStream::new, %0A%09%09%09%09 (baos, b) -%3E baos.write(b.byteValue()), %0A%09%09%09%09 (baos1, baos2) -%3E baos1.write(baos2.toByteArray(), 0, baos2.size())%0A%09%09%09)%0A%09%09%09.toByteArray();
 %0A%09%7D%0A%09%0A%09@
@@ -1974,16 +1974,20 @@
 .readers
+%0A%09%09%09
 .forEach
@@ -2002,22 +2002,24 @@
 -%3E %7B%0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09read
@@ -2030,16 +2030,17 @@
 lose();%0A
+%09
 %09%09%09%7D cat
@@ -2060,24 +2060,25 @@
 on e) %7B%0A%09%09%09%09
+%09
 throw new Ru
@@ -2099,20 +2099,18 @@
 (e);%0A%09%09%09
-%7D%0A%09
 %09
+%7D
 %7D);%09%09%0A%09%7D

Update MultiChannelByteReader.java