InputStreamInput implements Input { @Override public Iterator<String> iterator() { return new Iterator<String>() { private String line; private boolean pending = false; @Override public boolean hasNext() { if (pending) { return true; } line = readLine(); pending = true; return line != null; } @Override public String next() { if (!hasNext()) { throw new NoSuchElementException(); } pending = false; return line; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } InputStreamInput(InputStream inputStream);  InputStreamInput(BufferedReader reader); @Override int read(); @Override String readLine(); @Override Iterator<String> iterator();  }
@Test(groups = "fast") public void testIterator() throws Exception { ByteArrayInputStream inputStream = new ByteArrayInputStream("This is\na test\nof iteration\n".getBytes()); InputStreamInput input = new InputStreamInput(inputStream); List<String> lines = new ArrayList<>(); for (String line : input) { lines.add(line); } Assert.assertEquals(lines, Arrays.asList("This is", "a test", "of iteration")); }