List of usage examples for org.apache.commons.io IOUtils closeQuietly
public static void closeQuietly(OutputStream output)
OutputStream
. From source file:com.github.jdk7.Chapter1.java
public static void test1() { InputStream inputStream = Chapter1.class.getResourceAsStream("/dkj/a.txt"); try {/* w w w. j av a 2 s . c o m*/ String allLines = IOUtils.toString(inputStream); } catch (Throwable e) { System.err.println(" error "); } finally { IOUtils.closeQuietly(inputStream); } System.out.println("now"); System.out.println("close"); }
From source file:info.dolezel.fatrat.plugins.util.FileUtils.java
/** * Returns the first line from the file. * @return <code>null</code> if the operation failed for any reason. *///from w ww .j av a2 s . c o m public static String fileReadLine(String file) { FileInputStream fis = null; BufferedReader br = null; DataInputStream dis = null; InputStreamReader isr = null; try { fis = new FileInputStream(file); dis = new DataInputStream(fis); isr = new InputStreamReader(dis); br = new BufferedReader(isr); return br.readLine(); } catch (IOException ex) { return null; } finally { IOUtils.closeQuietly(fis); } }
From source file:hudson.init.InitScriptsExecutor.java
@Initializer(after = JOB_LOADED) public static void init(Hudson hudson) throws IOException { URL bundledInitScript = hudson.servletContext.getResource("/WEB-INF/init.groovy"); if (bundledInitScript != null) { logger.info("Executing bundled init script: " + bundledInitScript); InputStream in = bundledInitScript.openStream(); try {/*from w ww. j av a 2 s . c o m*/ String script = IOUtils.toString(in); logger.info(new Script(script).execute()); } finally { IOUtils.closeQuietly(in); } } File initScript = new File(hudson.getRootDir(), "init.groovy"); if (initScript.exists()) { execute(initScript); } File initScriptD = new File(hudson.getRootDir(), "init.groovy.d"); if (initScriptD.isDirectory()) { File[] scripts = initScriptD.listFiles(new FileFilter() { @Override public boolean accept(File f) { return f.getName().endsWith(".groovy"); } }); if (scripts != null) { // sort to run them in a deterministic order Arrays.sort(scripts); for (File f : scripts) { execute(f); } } } }
From source file:net.sf.keystore_explorer.utilities.io.ReadUtil.java
/** * Read all bytes from the supplied input stream. Closes the input stream. * * @param is//w w w . j a v a2 s .c om * Input stream * @return All bytes * @throws IOException * If an I/O problem occurs */ public static byte[] readFully(InputStream is) throws IOException { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int read = 0; while ((read = is.read(buffer)) != -1) { baos.write(buffer, 0, read); } return baos.toByteArray(); } finally { IOUtils.closeQuietly(baos); IOUtils.closeQuietly(is); } }
From source file:com.sap.prd.mobile.ios.mios.XCodeVersionUtil.java
public static String getXCodeVersionString() throws XCodeException { PrintStream out = null;//w w w . ja va 2 s . c om try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); out = new PrintStream(bos, true, Charset.defaultCharset().name()); int exitCode = Forker.forkProcess(out, new File("."), new String[] { "xcodebuild", "-version" }); if (exitCode == 0) { return bos.toString(Charset.defaultCharset().name()); } else { throw new XCodeException("Could not get xcodebuild version (exit code = " + exitCode + ")"); } } catch (Exception e) { throw new XCodeException("Could not get xcodebuild version"); } finally { IOUtils.closeQuietly(out); } }
From source file:com.hs.mail.util.FileUtils.java
public static boolean startsWith(File file, byte[] magic) { InputStream input = null;/*from w w w. jav a2 s . co m*/ try { int magicLen = magic.length; byte[] bytes = new byte[magicLen]; input = new FileInputStream(file); input.read(bytes, 0, magicLen); return new EqualsBuilder().append(magic, bytes).isEquals(); } catch (IOException e) { return false; } finally { IOUtils.closeQuietly(input); } }
From source file:com.textocat.textokit.morph.opencorpora.resource.GramModelDeserializer.java
public static GramModel from(InputStream in, String srcLabel) throws Exception { log.info("About to deserialize GramModel from InputStream of {}...", srcLabel); long timeBefore = currentTimeMillis(); InputStream is = new BufferedInputStream(in); ObjectInputStream ois = new ObjectInputStream(is); GramModel gm;/* www. j av a 2 s . c o m*/ try { gm = (GramModel) ois.readObject(); } finally { IOUtils.closeQuietly(ois); } log.info("Deserialization of GramModel finished in {} ms", currentTimeMillis() - timeBefore); return gm; }
From source file:gov.nih.nci.ncicb.tcga.dcc.dam.dao.FileComparer.java
public static void compareFiles(String fname1, String fname2) throws IOException { Reader r1 = null;/*from w w w . j a va 2s.c om*/ Reader r2 = null; try { File f1 = new File(fname1); File f2 = new File(fname2); //noinspection IOResourceOpenedButNotSafelyClosed r1 = new BufferedReader(new FileReader(f1)); //noinspection IOResourceOpenedButNotSafelyClosed r2 = new BufferedReader(new FileReader(f2)); int c1, c2; c1 = r1.read(); c2 = r2.read(); assertFalse("file1 has no content", c1 < 0); int i = 0; while (c1 != -1) { assertEquals("files are not equal at index " + i, c1, c2); c1 = r1.read(); c2 = r2.read(); ++i; } } finally { IOUtils.closeQuietly(r1); IOUtils.closeQuietly(r2); } }
From source file:com.lightboxtechnologies.nsrl.SmallTableLoader.java
protected static void load(String filename, LineHandler lh, RecordLoader loader) throws IOException { Reader r = null;//from w ww . j a v a 2 s. c o m try { r = new FileReader(filename); loader.load(r, lh); r.close(); } finally { IOUtils.closeQuietly(r); } }
From source file:net.orpiske.tcs.utils.compression.Decompressor.java
/** * Decompress an array of bytes/*from ww w . j av a 2 s . c o m*/ * @param bytes the array to decompress * @return a String object with the text * @throws IOException if unable to decompress it for any reason */ public static String decompress(final byte[] bytes) throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); InputStream gzipInputStream = new GZIPInputStream(inputStream); /** * Ok, this should be "smarter". Will fix this, eventually ... */ Reader reader = new InputStreamReader(gzipInputStream, Charset.forName("UTF-8")); BufferedReader bufferedReader = new BufferedReader(reader); StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1]; while (bufferedReader.read(buffer) > 0) { builder.append(buffer); } return builder.toString(); } finally { IOUtils.closeQuietly(gzipInputStream); IOUtils.closeQuietly(inputStream); } }