@Test public void testToInputStreamReadOneElementArraysByByteArray() throws IOException { InputStream in = StreamUtils.toInputStream(Stream.of(new byte[] { 3 }, new byte[] { 4 })); byte[] bytes = new byte[3]; assertEquals(1, in.read(bytes)); assertEquals(3, bytes[0]); assertEquals(1, in.read(bytes)); assertEquals(4, bytes[0]); }
public static InputStream toInputStream(Stream<? extends byte[]> stream) { return new InputStream() { StreamIterator<? extends byte[]> it = stream.iteratorNullChecked(); byte[] bytes = new byte[0]; int index; @Override public int read() throws IOException { load(); if (bytes == null) { return -1; } else { return bytes[index++] & 0xFF; } } @Override public int read(byte[] b, int off, int len) throws IOException { load(); if (bytes == null) { return -1; } else { int length = Math.min(len, bytes.length - index); System.arraycopy(bytes, index, b, off, length); index += length; return length; } } private void load() { if (bytes != null && index == bytes.length) { while (it.hasNext()) { bytes = it.nextNullChecked(); if (bytes.length > 0) { index = 0; return; } } bytes = null; it.dispose(); } } }; }
StreamUtils { public static InputStream toInputStream(Stream<? extends byte[]> stream) { return new InputStream() { StreamIterator<? extends byte[]> it = stream.iteratorNullChecked(); byte[] bytes = new byte[0]; int index; @Override public int read() throws IOException { load(); if (bytes == null) { return -1; } else { return bytes[index++] & 0xFF; } } @Override public int read(byte[] b, int off, int len) throws IOException { load(); if (bytes == null) { return -1; } else { int length = Math.min(len, bytes.length - index); System.arraycopy(bytes, index, b, off, length); index += length; return length; } } private void load() { if (bytes != null && index == bytes.length) { while (it.hasNext()) { bytes = it.nextNullChecked(); if (bytes.length > 0) { index = 0; return; } } bytes = null; it.dispose(); } } }; } }
StreamUtils { public static InputStream toInputStream(Stream<? extends byte[]> stream) { return new InputStream() { StreamIterator<? extends byte[]> it = stream.iteratorNullChecked(); byte[] bytes = new byte[0]; int index; @Override public int read() throws IOException { load(); if (bytes == null) { return -1; } else { return bytes[index++] & 0xFF; } } @Override public int read(byte[] b, int off, int len) throws IOException { load(); if (bytes == null) { return -1; } else { int length = Math.min(len, bytes.length - index); System.arraycopy(bytes, index, b, off, length); index += length; return length; } } private void load() { if (bytes != null && index == bytes.length) { while (it.hasNext()) { bytes = it.nextNullChecked(); if (bytes.length > 0) { index = 0; return; } } bytes = null; it.dispose(); } } }; } private  StreamUtils(); }
StreamUtils { public static InputStream toInputStream(Stream<? extends byte[]> stream) { return new InputStream() { StreamIterator<? extends byte[]> it = stream.iteratorNullChecked(); byte[] bytes = new byte[0]; int index; @Override public int read() throws IOException { load(); if (bytes == null) { return -1; } else { return bytes[index++] & 0xFF; } } @Override public int read(byte[] b, int off, int len) throws IOException { load(); if (bytes == null) { return -1; } else { int length = Math.min(len, bytes.length - index); System.arraycopy(bytes, index, b, off, length); index += length; return length; } } private void load() { if (bytes != null && index == bytes.length) { while (it.hasNext()) { bytes = it.nextNullChecked(); if (bytes.length > 0) { index = 0; return; } } bytes = null; it.dispose(); } } }; } private  StreamUtils(); static StreamIterator<T> iterator(StreamIterable<T> iter); static T next(StreamIterator<T> it); static InputStream toInputStream(Stream<? extends byte[]> stream); }
StreamUtils { public static InputStream toInputStream(Stream<? extends byte[]> stream) { return new InputStream() { StreamIterator<? extends byte[]> it = stream.iteratorNullChecked(); byte[] bytes = new byte[0]; int index; @Override public int read() throws IOException { load(); if (bytes == null) { return -1; } else { return bytes[index++] & 0xFF; } } @Override public int read(byte[] b, int off, int len) throws IOException { load(); if (bytes == null) { return -1; } else { int length = Math.min(len, bytes.length - index); System.arraycopy(bytes, index, b, off, length); index += length; return length; } } private void load() { if (bytes != null && index == bytes.length) { while (it.hasNext()) { bytes = it.nextNullChecked(); if (bytes.length > 0) { index = 0; return; } } bytes = null; it.dispose(); } } }; } private  StreamUtils(); static StreamIterator<T> iterator(StreamIterable<T> iter); static T next(StreamIterator<T> it); static InputStream toInputStream(Stream<? extends byte[]> stream);  }