public static long getFileCrc32(File file) throws IOException { InputStream inputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(file)); CRC32 crc = new CRC32(); int cnt; while ((cnt = inputStream.read()) != -1) { crc.update(cnt); } return crc.getValue(); } finally { StreamUtil.closeQuietly(inputStream); } }
public void executeAndSaveTo(File file) throws IOException { OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(file)); executeAndSaveTo(os); } finally { StreamUtil.closeQuietly(os); } } }
public void executeAndSaveTo(File file) throws IOException { OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(file)); executeAndSaveTo(os); } finally { StreamUtil.closeQuietly(os); } }
public static void copyResourceUsingStream(String name, File dest) throws IOException { FileOutputStream os = null; File parent = dest.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } InputStream is = null; try { is = FileOperation.class.getResourceAsStream("/" + name); os = new FileOutputStream(dest, false); byte[] buffer = new byte[TypedValue.BUFFER_SIZE]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { StreamUtil.closeQuietly(os); StreamUtil.closeQuietly(is); } }
public static void copyFileUsingStream(File source, File dest) throws IOException { FileInputStream is = null; FileOutputStream os = null; File parent = dest.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } try { is = new FileInputStream(source); os = new FileOutputStream(dest, false); byte[] buffer = new byte[TypedValue.BUFFER_SIZE]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { StreamUtil.closeQuietly(os); StreamUtil.closeQuietly(is); } }
public static long getFileSizes(File f) { if (f == null) { return 0; } long size = 0; if (f.exists() && f.isFile()) { InputStream fis = null; try { fis = new BufferedInputStream(new FileInputStream(f)); size = fis.available(); } catch (IOException e) { e.printStackTrace(); } finally { StreamUtil.closeQuietly(fis); } } return size; }
private static byte[] readContents(final File file) throws IOException { final ByteArrayOutputStream output = new ByteArrayOutputStream(); final int bufferSize = TypedValue.BUFFER_SIZE; InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); int length; byte[] buffer = new byte[bufferSize]; byte[] bufferCopy; while ((length = in.read(buffer, 0, bufferSize)) > 0) { bufferCopy = new byte[length]; System.arraycopy(buffer, 0, bufferCopy, 0, length); output.write(bufferCopy); } } finally { StreamUtil.closeQuietly(output); StreamUtil.closeQuietly(in); } return output.toByteArray(); }
/** * add zip entry * * @param zipOutputStream * @param zipEntry * @param inputStream * @throws Exception */ public static void addZipEntry(ZipOutputStream zipOutputStream, ZipEntry zipEntry, InputStream inputStream) throws Exception { try { zipOutputStream.putNextEntry(zipEntry); byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB]; int length = -1; while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) { zipOutputStream.write(buffer, 0, length); zipOutputStream.flush(); } } catch (ZipException e) { // do nothing } finally { StreamUtil.closeQuietly(inputStream); zipOutputStream.closeEntry(); } }
public void gen() throws Exception { addTinkerID(); Properties newProperties = new Properties(); for (String key : config.mPackageFields.keySet()) { newProperties.put(key, config.mPackageFields.get(key)); } String comment = "base package config field"; OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(packageInfoFile, false)); newProperties.store(os, comment); } finally { StreamUtil.closeQuietly(os); } } }
/** * save properties * * @param properties * @param outputFullFilename */ public static void saveProperties(Properties properties, String outputFullFilename) { if (properties != null && outputFullFilename != null) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(outputFullFilename); properties.store(outputStream, null); } catch (Exception e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(outputStream); } } }
public static String getMD5ExtendBytes(final File file, final int offset, final int length, byte[] extend) { if (file == null || !file.exists() || offset < 0 || length <= 0) { return null; } FileInputStream fin = null; try { fin = new FileInputStream(file); String md5 = MD5.getMD5ExtendBytes(fin, 1024 * 100, offset, length, extend); fin.close(); return md5; } catch (Exception e) { return null; } finally { StreamUtil.closeQuietly(fin); } } }
/** * Get the md5 for the file, using less memory. */ public static String getMD5(final File file, final int offset, final int length) { if (file == null || !file.exists() || offset < 0 || length <= 0) { return null; } FileInputStream fin = null; try { fin = new FileInputStream(file); String md5 = MD5.getMD5(fin, 1024 * 100, offset, length); fin.close(); return md5; } catch (Exception e) { return null; } finally { StreamUtil.closeQuietly(fin); } }
/** * read file * * @param fullFilename * @return byte[] */ public static byte[] readFile(String fullFilename) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InputStream inputStream = null; try { inputStream = new FileInputStream(fullFilename); copyStream(inputStream, byteArrayOutputStream); } catch (FileNotFoundException e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(byteArrayOutputStream); StreamUtil.closeQuietly(inputStream); } return byteArrayOutputStream.toByteArray(); }
/** * md5 file * * @param fullFilename * @return String */ public static String md5File(String fullFilename) { String result = null; if (fullFilename != null) { InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(fullFilename)); result = md5File(is); } catch (Exception e) { throw new RuntimeException(e); } finally { StreamUtil.closeQuietly(is); } } return result; }
/** * get properties * * @param propertiesFullFilename * @return Properties */ public static Properties getProperties(String propertiesFullFilename) { Properties properties = null; if (propertiesFullFilename != null) { InputStream inputStream = null; try { inputStream = new FileInputStream(propertiesFullFilename); properties = new Properties(); properties.load(inputStream); } catch (Exception e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(inputStream); } } return properties; }
/** * Get the md5 for the file. call getMD5(FileInputStream is, int bufLen) inside. * * @param file * @param bufLen bytes number read from the stream once. * The less bufLen is the more times getMD5() method takes. Also the less bufLen cost less memory. */ public static String getMD5(final File file, final int bufLen) { if (file == null || bufLen <= 0 || !file.exists()) { return null; } FileInputStream fin = null; try { fin = new FileInputStream(file); String md5 = MD5.getMD5(fin, (int) (bufLen <= file.length() ? bufLen : file.length())); fin.close(); return md5; } catch (Exception e) { return null; } finally { StreamUtil.closeQuietly(fin); } }
private String getSignatureAlgorithm() throws Exception { InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(config.mSignatureFile)); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(is, config.mStorePass.toCharArray()); Key key = keyStore.getKey(config.mStoreAlias, config.mKeyPass.toCharArray()); String keyAlgorithm = key.getAlgorithm(); String signatureAlgorithm; if (keyAlgorithm.equalsIgnoreCase("DSA")) { signatureAlgorithm = "SHA1withDSA"; } else if (keyAlgorithm.equalsIgnoreCase("RSA")) { signatureAlgorithm = "SHA1withRSA"; } else if (keyAlgorithm.equalsIgnoreCase("EC")) { signatureAlgorithm = "SHA1withECDSA"; } else { throw new RuntimeException("private key is not a DSA or " + "RSA key"); } return signatureAlgorithm; } finally { StreamUtil.closeQuietly(is); } }
/** * save document * * @param document * @param outputFullFilename */ public static void saveDocument(final Document document, final String outputFullFilename) { OutputStream outputStream = null; try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, Constant.Encoding.UTF8); outputStream = new FileOutputStream(outputFullFilename); StreamResult result = new StreamResult(outputStream); transformer.transform(domSource, result); } catch (Exception e) { throw new JavaXmlUtilException(e); } finally { StreamUtil.closeQuietly(outputStream); } }
/** * write file * * @param outputFullFilename * @param byteArray */ public static void writeFile(String outputFullFilename, byte[] byteArray) { InputStream inputStream = null; FileUtil.createFile(outputFullFilename); OutputStream outputStream = null; try { inputStream = new ByteArrayInputStream(byteArray); outputStream = new FileOutputStream(outputFullFilename); copyStream(inputStream, outputStream); } catch (FileNotFoundException e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(outputStream); StreamUtil.closeQuietly(inputStream); } }
/** * generate simple file * * @param templateFullFilename * @param outputFullFilename * @param valueMap */ public static void generateSimpleFile(String templateFullFilename, String outputFullFilename, Map<String, String> valueMap) { InputStream inputStream = null; try { inputStream = new FileInputStream(templateFullFilename); generateSimpleFile(inputStream, outputFullFilename, valueMap); } catch (Exception e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(inputStream); } }