/** * 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(); }
/** PUT request handler */ @RestMethod(name="PUT", path="/*", guards=AdminGuard.class) public Object doPut(RestRequest req) throws Exception { if (! allowPuts) throw new RestException(SC_METHOD_NOT_ALLOWED, "PUT not enabled"); File f = new File(rootDir.getAbsolutePath() + req.getPathInfo()); String parentSubPath = f.getParentFile().getAbsolutePath().substring(rootDir.getAbsolutePath().length()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f)); IOPipe.create(req.getInputStream(), bos).closeOut().run(); if (req.getContentType().contains("html")) return new Redirect(parentSubPath); return "File added"; }
/** * Writes the contents of the specified <code>InputStream</code> to the specified file. * * @param out The file to write the output to. * @param in The input stream to pipe from. * @return The number of characters written to the file. * @throws IOException */ public static int write(File out, InputStream in) throws IOException { assertFieldNotNull(out, "out"); assertFieldNotNull(in, "in"); try (OutputStream os = new FileOutputStream(out)) { return IOPipe.create(in, os).run(); } }
/** * Writes the contents of the specified <code>InputStream</code> to the specified file. * * @param out The file to write the output to. * @param in The input stream to pipe from. * @return The number of characters written to the file. * @throws IOException */ public static int write(File out, InputStream in) throws IOException { assertFieldNotNull(out, "out"); assertFieldNotNull(in, "in"); try (OutputStream os = new FileOutputStream(out)) { return IOPipe.create(in, os).run(); } }
/** * Writes the contents of the specified <code>InputStream</code> to the specified file. * * @param out The file to write the output to. * @param in The input stream to pipe from. * @return The number of characters written to the file. * @throws IOException */ public static int write(File out, InputStream in) throws IOException { assertFieldNotNull(out, "out"); assertFieldNotNull(in, "in"); try (OutputStream os = new FileOutputStream(out)) { return IOPipe.create(in, os).run(); } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res, Object output) throws IOException, RestException { if (output instanceof Reader) { Writer w = res.getNegotiatedWriter(); IOPipe.create(output, w).closeOut().run(); return true; } return false; } }
@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) {} }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res) throws IOException, NotAcceptable, RestException { if (res.isOutputType(Reader.class)) { try (Writer w = res.getNegotiatedWriter()) { IOPipe.create(res.getOutput(Reader.class), w).run(); } return true; } return false; } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res) throws IOException, NotAcceptable, RestException { if (res.isOutputType(Reader.class)) { try (Writer w = res.getNegotiatedWriter()) { IOPipe.create(res.getOutput(Reader.class), w).run(); } return true; } return false; } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res) throws IOException, NotAcceptable, RestException { if (res.isOutputType(Reader.class)) { try (Writer w = res.getNegotiatedWriter()) { IOPipe.create(res.getOutput(Reader.class), w).run(); } return true; } return false; } }
@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; } }
/** * Writes the contents of the specified <code>Reader</code> to the specified file. * * @param out The file to write the output to. * @param in The reader to pipe from. * @return The number of characters written to the file. * @throws IOException */ public static int write(File out, Reader in) throws IOException { assertFieldNotNull(out, "out"); assertFieldNotNull(in, "in"); try (Writer w = FileWriterBuilder.create(out).build()) { return IOPipe.create(in, w).run(); } }
/** * Writes the contents of the specified <code>Reader</code> to the specified file. * * @param out The file to write the output to. * @param in The reader to pipe from. * @return The number of characters written to the file. * @throws IOException */ public static int write(File out, Reader in) throws IOException { assertFieldNotNull(out, "out"); assertFieldNotNull(in, "in"); try (Writer w = FileWriterBuilder.create(out).build()) { return IOPipe.create(in, w).run(); } }
/** * Writes the contents of the specified <code>Reader</code> to the specified file. * * @param out The file to write the output to. * @param in The reader to pipe from. * @return The number of characters written to the file. * @throws IOException */ public static int write(File out, Reader in) throws IOException { assertFieldNotNull(out, "out"); assertFieldNotNull(in, "in"); try (Writer w = FileWriterBuilder.create(out).build()) { return IOPipe.create(in, w).run(); } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res) throws IOException, NotAcceptable, RestException { if (res.isOutputType(InputStream.class)) { res.setHeader("Content-Type", res.getContentType()); try (OutputStream os = res.getNegotiatedOutputStream()) { IOPipe.create(res.getOutput(InputStream.class), os).run(); } return true; } return false; } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res) throws IOException, NotAcceptable, RestException { if (res.isOutputType(InputStream.class)) { res.setHeader("Content-Type", res.getContentType()); try (OutputStream os = res.getNegotiatedOutputStream()) { IOPipe.create(res.getOutput(InputStream.class), os).run(); } return true; } return false; } }
@Override /* ResponseHandler */ public boolean handle(RestRequest req, RestResponse res) throws IOException, NotAcceptable, RestException { if (res.isOutputType(InputStream.class)) { res.setHeader("Content-Type", res.getContentType()); try (OutputStream os = res.getNegotiatedOutputStream()) { IOPipe.create(res.getOutput(InputStream.class), os).run(); } return true; } return false; } }
@Test public void testBasic() throws Exception { InputStream is = new ByteArrayInputStream("foobar".getBytes(UTF8)); ByteArrayInOutStream baios = new ByteArrayInOutStream(); IOPipe.create(is, baios).run(); assertEquals("foobar", read(baios.getInputStream())); } }
@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) {} } }
@Test public void testRead() throws Exception { TestReader in; TestWriter out; in = new TestReader("foobar"); out = new TestWriter(); IOPipe.create(in, out).run(); assertTrue(in.closed); assertFalse(out.closed); assertEquals("foobar", out.toString()); }