List of usage examples for org.apache.commons.io IOUtils closeQuietly
public static void closeQuietly(OutputStream output)
OutputStream
. From source file:com.adaptris.core.services.Base64EncodeService.java
/** * @see com.adaptris.core.Service#doService(com.adaptris.core.AdaptrisMessage) *//*from w w w . j a v a 2 s . c o m*/ public void doService(AdaptrisMessage msg) throws ServiceException { OutputStream out = null; InputStream in = null; try { in = msg.getInputStream(); out = MimeUtility.encode(msg.getOutputStream(), MimeConstants.ENCODING_BASE64); IOUtils.copy(in, out); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:com.haulmont.cuba.gui.ScreenViewsLoader.java
/** * Deploy views defined in <code>metadataContext</code> of a frame. * * @param rootElement root element of a frame XML *//*ww w.jav a2 s .com*/ public void deployViews(Element rootElement) { Element metadataContextEl = rootElement.element("metadataContext"); if (metadataContextEl != null) { for (Element fileEl : Dom4j.elements(metadataContextEl, "deployViews")) { String resource = fileEl.attributeValue("name"); InputStream resourceInputStream = getInputStream(resource); try { viewRepository.deployViews(resourceInputStream); } finally { IOUtils.closeQuietly(resourceInputStream); } } for (Element viewEl : Dom4j.elements(metadataContextEl, "view")) { viewRepository.deployView(metadataContextEl, viewEl); } } }
From source file:com.github.jknack.handlebars.io.ServletContextTemplateLoaderTest.java
@Test public void defaultLoad() throws IOException { InputStream is = createMock(InputStream.class); is.close();//w w w. java2s .c o m expectLastCall(); ServletContext servletContext = createMock(ServletContext.class); expect(servletContext.getResourceAsStream("/template.hbs")).andReturn(is); replay(servletContext, is); TemplateLoader locator = new ServletContextTemplateLoader(servletContext); Reader reader = locator.load(URI.create("template")); assertNotNull(reader); IOUtils.closeQuietly(reader); verify(servletContext, is); }
From source file:com.adaptris.core.services.Base64DecodeService.java
/** * @see com.adaptris.core.Service#doService(com.adaptris.core.AdaptrisMessage) *//*w w w . j a va 2 s .c o m*/ public void doService(AdaptrisMessage msg) throws ServiceException { OutputStream out = null; InputStream in = null; try { out = msg.getOutputStream(); in = MimeUtility.decode(msg.getInputStream(), MimeConstants.ENCODING_BASE64); IOUtils.copy(in, out); } catch (Exception e) { throw new ServiceException(e); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:com.doculibre.constellio.feedprotocol.parse.xml.FeedEntityResolver.java
private FeedEntityResolver() { InputStream inputStream = null; try {//from ww w .j a va 2 s . co m inputStream = DTD_PATH.getInputStream(); if (inputStream == null) { throw new IOException("Could not find : " + DTD_PATH.getPath()); } byteArray = IOUtils.toByteArray(inputStream); } catch (IOException ioe) { ioe.printStackTrace(); throw new RuntimeException(ioe); } finally { IOUtils.closeQuietly(inputStream); } }
From source file:com.photon.maven.plugins.android.common.JarHelper.java
/** Unjars the specified jar file into the the specified directory * * @param jarFile/* w w w . j a v a 2 s.c om*/ * @param outputDirectory * @param unjarListener * @throws IOException */ public static void unjar(JarFile jarFile, File outputDirectory, UnjarListener unjarListener) throws IOException { for (Enumeration en = jarFile.entries(); en.hasMoreElements();) { JarEntry entry = (JarEntry) en.nextElement(); File entryFile = new File(outputDirectory, entry.getName()); if (unjarListener.include(entry)) { if (!entryFile.getParentFile().exists() && !entryFile.getParentFile().mkdirs()) { throw new IOException("Error creating output directory: " + entryFile.getParentFile()); } // If the entry is an actual file, unzip that too if (!entry.isDirectory()) { final InputStream in = jarFile.getInputStream(entry); try { final OutputStream out = new FileOutputStream(entryFile); try { IOUtil.copy(in, out); } finally { IOUtils.closeQuietly(out); } } finally { IOUtils.closeQuietly(in); } } } } }
From source file:de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.ReportTools.java
/** * Prints macro F1 and F1 for each label into file (each line: label + F1, tab delimited) * * @param confusionMatrix matrix/* w w w . ja va 2 s. c o m*/ * @param file file * @throws IOException */ public static void printFMeasuresToFile(ConfusionMatrix confusionMatrix, File file) throws IOException { // and write to the output PrintWriter pw = new PrintWriter(file); // print macro F1 first pw.printf(Locale.ENGLISH, "%s\t%.3f%n", "Macro-Fm", confusionMatrix.getMacroFMeasure()); // then for all labels for (Map.Entry<String, Double> entry : confusionMatrix.getFMeasureForLabels().entrySet()) { pw.printf(Locale.ENGLISH, "%s\t%.3f%n", entry.getKey(), entry.getValue()); } IOUtils.closeQuietly(pw); }
From source file:com.evolveum.midpoint.tools.ninja.ImportDDL.java
public boolean execute() { System.out.println("Starting DDL import."); File script = new File(config.getFilePath()); if (!script.exists() || !script.canRead()) { System.out//from w w w .jav a2s. c om .println("DDL script file '" + script.getAbsolutePath() + "' doesn't exist or can't be read."); return false; } Connection connection = null; BufferedReader reader = null; try { connection = createConnection(); if (connection == null) { return false; } readScript(script, reader, connection); } catch (Exception ex) { System.out.println("Exception occurred, reason: " + ex.getMessage()); ex.printStackTrace(); } finally { IOUtils.closeQuietly(reader); try { if (connection != null && !connection.isClosed()) { connection.close(); } } catch (Exception ex) { System.out.println("Couldn't close JDBC connection, reason: " + ex.getMessage()); } } System.out.println("DDL import finished."); return true; }
From source file:com.glaf.chart.util.ChartUtils.java
public static byte[] createChart(Chart chartModel, JFreeChart chart) { ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; try {/* www .j av a 2 s . c o m*/ baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos); java.awt.image.BufferedImage bi = chart.createBufferedImage(chartModel.getChartWidth(), chartModel.getChartHeight()); if ("png".equalsIgnoreCase(chartModel.getImageType())) { EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(), bos); ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); ServletUtilities.saveChartAsPNG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(), info, null); } else if ("jpeg".equalsIgnoreCase(chartModel.getImageType())) { EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(), bos); ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); ServletUtilities.saveChartAsJPEG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(), info, null); } bos.flush(); baos.flush(); return baos.toByteArray(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeQuietly(baos); IOUtils.closeQuietly(bos); } }