List of usage examples for java.io InputStream close
public void close() throws IOException
From source file:org.apache.openejb.arquillian.tests.jaxrs.JaxrsTest.java
public static String asString(HttpResponse execute) throws IOException { final InputStream in = execute.getEntity().getContent(); try {/*from w w w. j a v a 2 s . com*/ return IO.slurp(in); } finally { in.close(); } }
From source file:com.nridge.core.base.io.IO.java
public static void closeQuietly(InputStream aStream) { if (aStream != null) { try {/*from w ww . jav a 2 s .c o m*/ aStream.close(); } catch (Exception ignored) { } } }
From source file:hudson.plugins.parameterizedtrigger.ParameterizedTriggerUtils.java
/** * {@link FilePath#readToString()} with encoding. * //ww w. j a v a 2 s.c o m * @param f file to read * @param encoding null for platform default encoding. * @return read string * @throws IOException */ public static String readFileToString(FilePath f, String encoding) throws IOException { InputStream in = f.read(); try { return IOUtils.toString(in, encoding); } finally { in.close(); } }
From source file:Main.java
/** * Reads Properties from given inputStream and returns it. * NOTE: the given stream is closed by this method *///from w w w. j av a2 s . c o m public static Properties readProperties(InputStream is, Properties props) throws IOException { if (props == null) props = new Properties(); try { props.load(is); } finally { is.close(); } return props; }
From source file:Main.java
public static void copyAndClose(InputStream in, OutputStream out) throws IOException { byte[] cache = new byte[4096]; int size;//from w w w. ja v a 2 s . c om while ((size = in.read(cache)) > 0) { out.write(cache, 0, size); } in.close(); out.close(); }
From source file:Main.java
public static String slurp(File file) throws IOException { String body = ""; InputStream is = null; try {/*from w ww . j ava 2 s .c o m*/ is = new FileInputStream(file); body = slurp(is); } finally { if (is != null) { is.close(); } } return body; }
From source file:org.opendaylight.atrium.hostservice.impl.ConfigReader.java
public static boolean initialize(URL configFileUrl) { byte[] jsonData = null; if (configFileUrl != null) { try {//from w w w. j av a 2s . c o m InputStream input = configFileUrl.openStream(); jsonData = IOUtils.toByteArray(input); input.close(); } catch (IOException e) { e.printStackTrace(); } } ObjectMapper objectMapper = new ObjectMapper(); try { rootJsonNode = objectMapper.readTree(jsonData); } catch (IOException e) { return false; } return true; }
From source file:edu.scripps.fl.test.pubchem.test.FetchFromDepositionSystemTest.java
private static void displayFile(int aid, InputStream is, String ext) throws IOException { File file = File.createTempFile("AID" + aid + "-", "." + ext); file.deleteOnExit();/* w ww.ja va 2 s . com*/ IOUtils.copy(is, new FileOutputStream(file)); is.close(); log.info("Created file " + file); // Desktop.getDesktop().open(file); }
From source file:com.aurel.track.exchange.latex.exporter.LaTeXExportBL.java
/** * Serializes the docx content into the response's output stream * @param response//from www. j av a 2 s . c o m * @param wordMLPackage * @return */ public static String prepareReportResponse(HttpServletResponse response, TWorkItemBean workItem, ReportBeans reportBeans, TPersonBean user, Locale locale, String templateDir, String templateFile) { ReportBeansToLaTeXConverter rl = new ReportBeansToLaTeXConverter(); File pdf = rl.generatePdf(workItem, reportBeans.getItems(), true, locale, user, "", "", false, new File(templateFile), new File(templateDir)); String fileName = workItem.getSynopsis() + ".pdf"; String contentType = "application/pdf"; if (pdf.length() < 10) { pdf = new File(pdf.getParent() + "/errors.txt"); fileName = workItem.getSynopsis() + ".txt"; contentType = "text"; } OutputStream outputStream = null; try { response.reset(); response.setHeader("Content-Type", contentType); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); DownloadUtil.prepareCacheControlHeader(ServletActionContext.getRequest(), response); outputStream = response.getOutputStream(); InputStream is = new FileInputStream(pdf); IOUtils.copy(is, outputStream); is.close(); } catch (FileNotFoundException e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } catch (IOException e) { LOGGER.error("Getting the output stream failed with " + e.getMessage()); LOGGER.error(ExceptionUtils.getStackTrace(e)); } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } // Docx4J.save(wordMLPackage, outputStream, Docx4J.FLAG_NONE); // //wordMLPackage.save(outputStream); // /*SaveToZipFile saver = new SaveToZipFile(wordMLPackage); // saver.save(outputStream);*/ // } catch (Exception e) { // LOGGER.error("Exporting the docx failed with throwable " + e.getMessage()); // LOGGER.debug(ExceptionUtils.getStackTrace(e)); // } return null; }
From source file:Main.java
/** * Read a file as an XML document./*from w w w . j a v a2 s.c om*/ * @param file * @return the XML document */ public static Document readXmlDocumentFromFile(File file) { try { InputStream inputStream = new FileInputStream(file); try { return readXmlDocumentFromStream(inputStream); } finally { inputStream.close(); } } catch (IOException e) { throw new RuntimeException(e); } }