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); } }
/** * 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 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 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 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); } }
/** * 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); } }
/** * 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; }
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; }
/** * 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); } }
/** * 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; }
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); } } }
/** * md5 file * * @param inputStream * @return String */ public static String md5File(final InputStream inputStream) { String result = null; if (inputStream != null) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB]; int readCount = 0; while ((readCount = inputStream.read(buffer, 0, buffer.length)) != -1) { md.update(buffer, 0, readCount); } result = StringUtil.byteToHexString(md.digest()); } catch (Exception e) { e.printStackTrace(); } finally { StreamUtil.closeQuietly(inputStream); } } return result; } }
/** * 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); } }
/** * 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); } }
static RetryInfo readRetryProperty(File infoFile) { String md5 = null; String times = null; Properties properties = new Properties(); FileInputStream inputStream = null; try { inputStream = new FileInputStream(infoFile); properties.load(inputStream); md5 = properties.getProperty(RETRY_FILE_MD5_PROPERTY); times = properties.getProperty(RETRY_COUNT_PROPERTY); } catch (IOException e) { TinkerLog.e(TAG, "fail to readRetryProperty:" + e); } finally { StreamUtil.closeQuietly(inputStream); } return new RetryInfo(md5, times); }
/** * merge file * * @param outputFullFilename * @param fullFilenameList */ public static void mergeFile(String outputFullFilename, List<String> fullFilenameList) { if (fullFilenameList != null && outputFullFilename != null) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(outputFullFilename); for (String fullFilename : fullFilenameList) { InputStream inputStream = null; try { inputStream = new FileInputStream(fullFilename); copyStream(inputStream, outputStream); } catch (Exception e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(inputStream); } } } catch (Exception e) { throw new FileUtilException(e); } finally { StreamUtil.closeQuietly(outputStream); } } }