### Question:
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);  }### Answer:
@Test public void testToInputStreamEmptyRead() throws IOException { InputStream in = StreamUtils.toInputStream(Stream.empty()); assertEquals(-1, in.read()); assertEquals(-1, in.read()); }
@Test public void testToInputStreamEmptyReadByteArray() throws IOException { InputStream in = StreamUtils.toInputStream(Stream.empty()); byte[] bytes = new byte[3]; assertEquals(-1, in.read(bytes)); assertEquals(-1, in.read(bytes)); }
@Test public void testToInputStreamEmptyByteArrayInputs() throws IOException { InputStream in = StreamUtils.toInputStream(Stream.of(new byte[] {}, new byte[] {})); assertEquals(-1, in.read()); }
@Test public void testToInputStreamEmptyByteArrayInputs2() throws IOException { InputStream in = StreamUtils.toInputStream(Stream.of(new byte[] {}, new byte[] {})); byte[] bytes = new byte[3]; assertEquals(-1, in.read(bytes)); assertEquals(-1, in.read(bytes)); }
@Test public void testToInputStreamReadOneElementArraysByByte() throws IOException { InputStream in = StreamUtils.toInputStream(Stream.of(new byte[] { 1 }, new byte[] { 2 })); assertEquals(1, in.read()); assertEquals(2, in.read()); assertEquals(-1, in.read()); }
@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]); }