### Question:
StreamGobbler extends Thread implements Closeable { @Override public void close() { running.set(false); interrupt(); } StreamGobbler(InputStream stream, OutputStream out, Logger log);  StreamGobbler(InputStream stream, PrintStream out, Logger log); @Override void close(); @Deprecated void shutdown(); StreamGobbler setPrefix(String prefix); StreamGobbler setPrintPrefix(String prefix); StreamGobbler setLogPrefix(String prefix); @Override void run(); void onChar(int c); void onLine(String line); void onClose(); void blockUntilFinished(); boolean isFinished();  }### Answer:
@Test public void testGobbleStream() throws Exception { byte[] bytes = new byte[] {'a','b','c'}; InputStream stream = new ByteArrayInputStream(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamGobbler gobbler = new StreamGobbler(stream, out, null); gobbler.start(); try { gobbler.join(10*1000); assertFalse(gobbler.isAlive()); assertEquals(new String(out.toByteArray()), "abc" + NL); } finally { gobbler.close(); gobbler.interrupt(); } }
@Test public void testGobbleMultiLineBlockingStream() throws Exception { PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream stream = new PipedInputStream(pipedOutputStream); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamGobbler gobbler = new StreamGobbler(stream, out, null); gobbler.start(); try { pipedOutputStream.write("line1\n".getBytes()); assertEqualsEventually(out, "line1" + NL); pipedOutputStream.write("line2\n".getBytes()); assertEqualsEventually(out, "line1" + NL + "line2" + NL); pipedOutputStream.write("line".getBytes()); pipedOutputStream.write("3\n".getBytes()); assertEqualsEventually(out, "line1" + NL + "line2" + NL + "line3" + NL); pipedOutputStream.close(); gobbler.join(10*1000); assertFalse(gobbler.isAlive()); assertEquals(new String(out.toByteArray()), "line1" + NL + "line2" + NL + "line3" + NL); } finally { gobbler.close(); gobbler.interrupt(); } }
@Test public void testGobbleMultiLineBlockingStream() throws Exception { PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream stream = new PipedInputStream(pipedOutputStream); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamGobbler gobbler = new StreamGobbler(stream, out, null); gobbler.start(); try { pipedOutputStream.write("line1\n".getBytes()); pipedOutputStream.flush(); assertEqualsEventually(out, "line1" + NL); pipedOutputStream.write("line2\n".getBytes()); pipedOutputStream.flush(); assertEqualsEventually(out, "line1" + NL + "line2" + NL); pipedOutputStream.write("line".getBytes()); pipedOutputStream.write("3\n".getBytes()); pipedOutputStream.flush(); assertEqualsEventually(out, "line1" + NL + "line2" + NL + "line3" + NL); pipedOutputStream.close(); gobbler.join(10*1000); assertFalse(gobbler.isAlive()); assertEquals(new String(out.toByteArray()), "line1" + NL + "line2" + NL + "line3" + NL); } finally { gobbler.close(); gobbler.interrupt(); } }