List of usage examples for org.apache.commons.io IOUtils closeQuietly
public static void closeQuietly(OutputStream output)
OutputStream
. From source file:invio.util.ArquivoUtil.java
public static void exportaPDF(String path) { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); response.setContentType("application/pdf"); try {//from w w w. j ava 2 s.c om FileInputStream input = new FileInputStream(path); byte[] bytes = IOUtils.toByteArray(input); OutputStream output = response.getOutputStream(); output.write(bytes); IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } catch (IOException e) { } }
From source file:com.github.dactiv.common.utils.PropertiesUtils.java
/** * properties, ???.//from w w w . ja v a2 s. c om * Spring Resource?, ?UTF-8. * * @param resourcesPaths Spring Resource path */ public static Properties loadProperties(String... resourcesPaths) { Properties props = new Properties(); for (String location : resourcesPaths) { logger.debug("Loading properties file from:" + location); InputStream is = null; try { Resource resource = resourceLoader.getResource(location); is = resource.getInputStream(); propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING)); } catch (IOException ex) { logger.info("Could not load properties from classpath:" + location + ": " + ex.getMessage()); } finally { IOUtils.closeQuietly(is); } } return props; }
From source file:net.landora.video.info.file.FileHasher.java
public static String getED2KHash(File file) { InputStream is = null;//w w w.ja v a 2 s.c om try { is = new BufferedInputStream(new FileInputStream(file)); Edonkey e2dkChecksum = new Edonkey(); IOUtils.copy(is, new CheckedOutputStream(new NullOutputStream(), e2dkChecksum)); return e2dkChecksum.getHexValue(); } catch (Exception e) { LoggerFactory.getLogger(FileHasher.class).error("Error hashing file.", e); return null; } finally { IOUtils.closeQuietly(is); } }
From source file:com.fluidops.iwb.api.helper.RDFUtils.java
public static List<Statement> readStatements(File rdfXmlFile) { FileReader rdfXmlReader = null; try {/*from w ww. j av a 2 s . c om*/ rdfXmlReader = new FileReader(rdfXmlFile); return readStatements(rdfXmlReader); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } finally { IOUtils.closeQuietly(rdfXmlReader); } }
From source file:com.lushapp.common.web.utils.DownloadUtils.java
public static void download(HttpServletRequest request, HttpServletResponse response, String filePath, String displayName) throws IOException { File file = new File(filePath); if (StringUtils.isEmpty(displayName)) { displayName = file.getName();//from w w w. j a va2s. com } if (!file.exists() || !file.canRead()) { response.setContentType("text/html;charset=utf-8"); response.getWriter().write("??"); return; } response.reset(); WebUtils.setNoCacheHeader(response); response.setContentType("application/x-download"); response.setContentLength((int) file.length()); String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1); displayFilename = displayFilename.replace(" ", "_"); WebUtils.setDownloadableHeader(request, response, displayFilename); BufferedInputStream is = null; OutputStream os = null; try { os = response.getOutputStream(); is = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(is, os); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } }
From source file:com.oprisnik.semdroid.app.parser.manifest.AXMLConverter.java
public static String getManifestString(byte[] apk) { String result = null;/* w w w .ja v a 2s .c o m*/ ZipInputStream zis = null; ByteArrayInputStream fis = null; try { fis = new ByteArrayInputStream(apk); zis = new ZipInputStream(fis); for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) { if (entry.getName().equals("AndroidManifest.xml")) { result = getManifestString(zis); break; } } } catch (FileNotFoundException e) { IOUtils.closeQuietly(fis); } catch (IOException e) { } finally { IOUtils.closeQuietly(zis); } return result; }
From source file:de.qaware.chronix.converter.common.Compression.java
/** * Compresses the given byte[]//w ww . j a va 2s. c o m * * @param decompressed - the byte[] to compress * @return the byte[] compressed */ public static byte[] compress(byte[] decompressed) { if (decompressed == null) { return new byte[] {}; } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(decompressed.length); OutputStream gzipOutputStream = null; try { gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(decompressed); gzipOutputStream.flush(); byteArrayOutputStream.flush(); } catch (IOException e) { LOGGER.error("Exception occurred while compressing gzip stream.", e); return null; } finally { IOUtils.closeQuietly(gzipOutputStream); IOUtils.closeQuietly(byteArrayOutputStream); } return byteArrayOutputStream.toByteArray(); }
From source file:com.sangupta.jerry.unsafe.UnsafeMemoryUtils.java
public static void writeToFile(UnsafeMemory memory, File file) throws IOException { FileOutputStream stream = null; BufferedOutputStream boss = null; try {/*from w w w . jav a 2 s . c om*/ final byte[] bytes = memory.getBuffer(); final int length = memory.getPosition(); stream = new FileOutputStream(file); boss = new BufferedOutputStream(stream); int CHUNK_SIZE = 1 << 20; int chunks = length / CHUNK_SIZE; int extra = length % CHUNK_SIZE; if (extra > 0) { chunks++; } for (int ki = 0; ki < chunks; ki++) { int delta = CHUNK_SIZE; if ((ki + 1) == chunks) { delta = extra; } boss.write(bytes, ki * CHUNK_SIZE, delta); } } catch (IOException e) { throw new IOException("Unable to write bytes to disk", e); } finally { IOUtils.closeQuietly(boss); IOUtils.closeQuietly(stream); } }
From source file:com.textocat.textokit.morph.opencorpora.resource.DictionaryDeserializer.java
public static MorphDictionaryImpl from(InputStream in, String srcLabel) throws Exception { log.info("About to deserialize MorphDictionary from InputStream of {}...", srcLabel); long timeBefore = currentTimeMillis(); InputStream is = new BufferedInputStream(in, DICTIONARY_READING_BUFFER_SIZE); ObjectInputStream ois = new ObjectInputStream(is); MorphDictionaryImpl dict;/*w w w .j ava 2 s . co m*/ try { // skip gram model @SuppressWarnings("unused") GramModel gm = (GramModel) ois.readObject(); dict = (MorphDictionaryImpl) ois.readObject(); } finally { IOUtils.closeQuietly(ois); } log.info("Deserialization of MorphDictionary finished in {} ms", currentTimeMillis() - timeBefore); return dict; }
From source file:com.google.gwt.site.uploader.HashCalculatorSha1Impl.java
@Override public String calculateHash(File file) throws IOException { FileInputStream fis = null;// ww w . j av a 2s. co m try { fis = new FileInputStream(file); return DigestUtils.sha1Hex(fis); } finally { IOUtils.closeQuietly(fis); } }