/** * Pipes the contents of the specified object into the writer. * * <p> * The reader is closed, the writer is not. * * @param in * The input to pipe from. * Can be any of the types defined by {@link #toReader(Object)}. * @param out * The writer to pipe to. * @throws IOException */ public static void pipe(Object in, Writer out) throws IOException { IOPipe.create(in, out).run(); }
/** * Creates a new pipe with the specified input and output. * * @param input The input. Must be one of the following types: Reader, InputStream, CharSequence. * @param output The output. Must be one of the following types: Writer, OutputStream. * @return This object (for method chaining). */ public static IOPipe create(Object input, Object output) { return new IOPipe(input, output); }
/** * Run this command and pipes the output to the specified writer or output stream. * * @return The exit code from the process. * @throws IOException * @throws InterruptedException */ public int run() throws IOException, InterruptedException { if (pb.command().size() == 0) throw new IOException("No command specified in ProcBuilder."); try { logWriters.append(divider).append('\n').flush(); logWriters.append(join(pb.command(), " ")).append('\n').flush(); p = pb.start(); IOPipe.create(p.getInputStream(), outWriters).lineProcessor(lp).byLines(byLines).run(); int rc = p.waitFor(); logWriters.append("Exit: ").append(String.valueOf(p.exitValue())).append('\n').flush(); if (rc > maxExitStatus) throw new IOException("Return code "+rc+" from command " + join(pb.command(), " ")); return rc; } finally { close(); } }
@Test public void testBuffSize() throws Exception { TestReader in; TestWriter out; in = new TestReader("foobar"); out = new TestWriter(); IOPipe.create(in, out).buffSize(1).run(); assertEquals("foobar", out.toString().replaceAll("[\\r\\n]", "")); try { IOPipe.create(in, out).buffSize(0); fail(); } catch (IllegalArgumentException e) {} } }
IOPipe.create(in, out).lineProcessor(lp).run(); assertEquals("[foo][bar]", out.toString().replaceAll("[\\r\\n]", "")); in = new TestReader("foo\nbar"); out = new TestWriter(); IOPipe.create(in, out).lineProcessor(lp2).run(); assertEquals("bar", out.toString().replaceAll("[\\r\\n]", "")); in2 = new TestInputStream("foo\nbar"); out2 = new TestOutputStream(); IOPipe.create(in2, out2).lineProcessor(lp).run(); assertEquals("[foo][bar]", out2.toString().replaceAll("[\\r\\n]", ""));
IOPipe.create(in, out).run(); assertTrue(in.closed); assertFalse(out.closed); IOPipe.create(in, out).closeOut().run(); assertTrue(in.closed); assertTrue(out.closed); IOPipe.create(in, out).close(false, true).run(); assertFalse(in.closed); assertTrue(out.closed); IOPipe.create(in2, out2).run(); assertTrue(in2.closed); assertFalse(out2.closed); IOPipe.create(in2, out2).closeOut().run(); assertTrue(in2.closed); assertTrue(out2.closed); IOPipe.create(in2, out2).close(false, true).run(); assertFalse(in2.closed); assertTrue(out2.closed); IOPipe.create(in, out2).run(); assertTrue(in.closed); assertFalse(out2.closed); IOPipe.create(in, out2).closeOut().run();
@Test public void testConstructor() throws Exception { try { IOPipe.create(null, new StringWriter()); fail(); } catch (IllegalArgumentException e) {} try { IOPipe.create(new StringReader(""), null); fail(); } catch (IllegalArgumentException e) {} try { IOPipe.create(new Integer(1), new StringWriter()); fail(); } catch (IllegalArgumentException e) {} try { IOPipe.create("", new Integer(1)); fail(); } catch (IllegalArgumentException e) {} }
closeQuietly(input, output);
/** * Run this command and pipes the output to the specified writer or output stream. * * @return The exit code from the process. * @throws IOException * @throws InterruptedException */ public int run() throws IOException, InterruptedException { if (pb.command().size() == 0) throw new IOException("No command specified in ProcBuilder."); try { logWriters.append(divider).append('\n').flush(); logWriters.append(join(pb.command(), " ")).append('\n').flush(); p = pb.start(); IOPipe.create(p.getInputStream(), outWriters).lineProcessor(lp).byLines(byLines).run(); int rc = p.waitFor(); logWriters.append("Exit: ").append(String.valueOf(p.exitValue())).append('\n').flush(); if (rc > maxExitStatus) throw new IOException("Return code "+rc+" from command " + join(pb.command(), " ")); return rc; } finally { close(); } }
closeQuietly(input, output);
/** * Pipes the contents of the specified object into the writer. * * <p> * The reader is closed, the writer is not. * * @param in * The input to pipe from. * Can be any of the types defined by {@link #toReader(Object)}. * @param out * The writer to pipe to. * @throws IOException */ public static void pipe(Object in, Writer out) throws IOException { IOPipe.create(in, out).run(); }
/** * Run this command and pipes the output to the specified writer or output stream. * * @return The exit code from the process. * @throws IOException * @throws InterruptedException */ public int run() throws IOException, InterruptedException { if (pb.command().size() == 0) throw new IOException("No command specified in ProcBuilder."); try { logWriters.append(divider).append('\n').flush(); logWriters.append(join(pb.command(), " ")).append('\n').flush(); p = pb.start(); IOPipe.create(p.getInputStream(), outWriters).lineProcessor(lp).byLines(byLines).run(); int rc = p.waitFor(); logWriters.append("Exit: ").append(String.valueOf(p.exitValue())).append('\n').flush(); if (rc > maxExitStatus) throw new IOException("Return code "+rc+" from command " + join(pb.command(), " ")); return rc; } finally { close(); } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res, Object output) throws IOException, RestException { if (output instanceof InputStream) { res.setHeader("Content-Type", res.getContentType()); OutputStream os = res.getNegotiatedOutputStream(); IOPipe.create(output, os).closeOut().run(); return true; } return false; } }
/** * Creates a new pipe with the specified input and output. * * @param input The input. Must be one of the following types: Reader, InputStream, CharSequence. * @param output The output. Must be one of the following types: Writer, OutputStream. * @return This object (for method chaining). */ public static IOPipe create(Object input, Object output) { return new IOPipe(input, output); }
closeQuietly(input, output);
/** * Pipes the contents of the specified object into the output stream. * * <p> * The input stream is closed, the output stream is not. * * @param in * The input to pipe from. * Can be any of the types defined by {@link #toInputStream(Object)}. * @param out * The writer to pipe to. * @throws IOException */ public static void pipe(Object in, OutputStream out) throws IOException { IOPipe.create(in, out).run(); }