private void send(InputStream input) throws IOException { try { // by calling httpServletResponse.getOutputStream() we are committing the response IoUtils.copy(input, httpServletResponse.getOutputStream()); if (chunked) { // flushing the buffer forces chunked-encoding httpServletResponse.flushBuffer(); } } finally { IoUtils.close(input); } }
/** * Shortcut to * {@code * getResourceAsString(getClass(), resource); * } */ public static String getResourceAsString(String resource) throws IOException { return getResourceAsString(IoUtils.class, resource); }
/** * Retrieve {@link String} for the file located in resources. * {@code * String html = getResourceAsString(MyClass.class, "/index.html"); * } */ public static String getResourceAsString(Class<?> clazz, String resource) throws IOException { try (InputStream inputStream = clazz.getResourceAsStream(resource)) { return IoUtils.toString(inputStream); } }
public static long copy(InputStream input, File file) throws IOException { try (FileOutputStream output = new FileOutputStream(file)) { return copy(input, output); } }
protected Class<?> defineClass(String className, InputStream is) { try { return defineClass(className, IoUtils.getBytes(is)); } catch (IOException e) { throw new IllegalStateException(e); } }
public static long copy(InputStream input, Writer writer) throws IOException { return copy(new InputStreamReader(input, StandardCharsets.UTF_8), writer); }
public static String toString(InputStream input) throws IOException { StringWriter writer = new StringWriter(); copy(input, writer); return writer.toString(); }
public String getBody() { if (body == null) { String httpServletRequestContentType = httpServletRequest.getHeader(HttpConstants.Header.CONTENT_TYPE); if (HttpConstants.Method.POST.equals(httpServletRequest.getMethod()) && (HttpConstants.ContentType.APPLICATION_FORM_URLENCODED.equals(httpServletRequestContentType) || HttpConstants.ContentType.MULTIPART_FORM_DATA.equals(httpServletRequestContentType))) { // Allow forms to exercise RESTful API endpoints by POSTing content like 'application/json'. // This parameter is usually paired with '_method' and '_content_type' parameters. body = getParameter("_content").toString(null); } else { try { body = IoUtils.toString(httpServletRequest.getInputStream()); } catch (Exception e) { throw new PippoRuntimeException(e, "Exception when reading the request body"); } } } return body; }
/** * Saves this file item to a given file on the server side. * * @param file * @throws IOException */ public void write(File file) throws IOException { try (InputStream inputStream = getInputStream()) { IoUtils.copy(inputStream, file); } }
String content; try { content = IoUtils.toString(input); } catch (IOException e) { throw new RuntimeException("Error loading the whitelist input", e);