List of usage examples for org.apache.commons.io IOUtils closeQuietly
public static void closeQuietly(OutputStream output)
OutputStream
. From source file:com.yiji.openapi.sdk.util.Servlets.java
public static void writeResponse(HttpServletResponse response, String data) { OutputStream output = null;//from ww w . j a v a 2 s. co m InputStream input = null; try { response.setCharacterEncoding("UTF-8"); response.setContentType(MediaType.APPLICATION_JSON_VALUE); output = response.getOutputStream(); input = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8"))); IOUtils.copy(input, output); output.flush(); } catch (Exception e) { throw new RuntimeException("?(flushResponse):" + e.getMessage()); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } }
From source file:fitnesse.testutil.SimpleSocketDonor.java
public void finishedWithSocket() { finished = true; IOUtils.closeQuietly(socket); }
From source file:latexstudio.editor.remote.DbxUtil.java
public static File downloadRemoteFile(DbxEntryDto remoteEntry, String localPath) { DbxClient client = getDbxClient();/*from w w w . j a v a2 s. c om*/ FileOutputStream outputStream = null; File outputFile = new File(localPath); try { outputStream = new FileOutputStream(outputFile); client.getFile(remoteEntry.getPath(), remoteEntry.getRevision(), outputStream); } catch (DbxException | IOException ex) { Exceptions.printStackTrace(ex); } finally { IOUtils.closeQuietly(outputStream); } return outputFile; }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.JCasIOHelper.java
/** * Loads JCas from a xmi file given the current typesystem on classpath (not lenient) * * @param file file/*from ww w .j a v a2 s. c o m*/ * @return jcas * @throws Exception */ public static JCas loadJCasFromFile(File file) throws Exception { CAS cas = CasCreationUtils.createCas(TypeSystemDescriptionFactory.createTypeSystemDescription(), null, null); FileInputStream in = new FileInputStream(file); XmiCasDeserializer.deserialize(in, cas, false); IOUtils.closeQuietly(in); return cas.getJCas(); }
From source file:com.alta189.deskbin.util.CryptUtils.java
private static boolean loadKey() { FileInputStream fis = null;//w ww .ja va 2s .c o m ObjectInputStream in = null; try { fis = new FileInputStream(keyFile); in = new ObjectInputStream(fis); Object obj = in.readObject(); if (obj != null && obj instanceof SecretKey) { key = (SecretKey) obj; return true; } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(in); } return false; }
From source file:com.thoughtworks.go.util.ObjectUtil.java
public static Object readObject(File file) throws ClassNotFoundException, IOException { InputStream inputStream = null; try {/* w ww . j a va 2s . com*/ inputStream = new FileInputStream(file); return new ObjectInputStream(inputStream).readObject(); } finally { IOUtils.closeQuietly(inputStream); } }
From source file:com.moz.fiji.hive.utils.FijiDataRequestSerializer.java
/** * Serializes a data request./* w ww. jav a2 s . c om*/ * * @param dataRequest A data request. * @return The serialized data request. * @throws IOException If there is an error. */ public static String serialize(FijiDataRequest dataRequest) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(baos); try { oos.writeObject(dataRequest); oos.flush(); return Base64.encodeBase64String(baos.toByteArray()); } finally { IOUtils.closeQuietly(oos); } }
From source file:Log4jPropertyHelper.java
public static void updateLog4jConfiguration(Class<?> targetClass, String log4jPath) throws Exception { Properties customProperties = new Properties(); FileInputStream fs = null;// w w w. jav a2 s. c om InputStream is = null; try { fs = new FileInputStream(log4jPath); is = targetClass.getResourceAsStream("/log4j.properties"); customProperties.load(fs); Properties originalProperties = new Properties(); originalProperties.load(is); for (Entry<Object, Object> entry : customProperties.entrySet()) { originalProperties.setProperty(entry.getKey().toString(), entry.getValue().toString()); } LogManager.resetConfiguration(); PropertyConfigurator.configure(originalProperties); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(fs); } }
From source file:com.formkiq.core.util.Zips.java
/** * Extract Zip file to Map./*from w w w. j a v a 2 s . c o m*/ * @param bytes byte[] * @return {@link Map} * @throws IOException IOException */ public static Map<String, byte[]> extractZipToMap(final byte[] bytes) throws IOException { Map<String, byte[]> map = new HashMap<>(); ByteArrayInputStream is = new ByteArrayInputStream(bytes); ZipInputStream zipStream = new ZipInputStream(is); try { ZipEntry entry = null; while ((entry = zipStream.getNextEntry()) != null) { String filename = entry.getName(); byte[] data = IOUtils.toByteArray(zipStream); map.put(filename, data); } } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(zipStream); } return map; }
From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java
/** * toString/*from w w w . j a v a 2 s.c o m*/ * @param props * @param comment * @param encoding * @return * @throws UnsupportedEncodingException */ public static String toString(Properties props, String comment, String encoding) throws UnsupportedEncodingException { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); props.store(baos, comment); baos.flush(); return new String(baos.toByteArray(), encoding); } catch (UnsupportedEncodingException e) { throw e; } catch (IOException e) { throw new Error("An IOException while working with byte array streams?!", e); } finally { IOUtils.closeQuietly(baos); } }