use of java.io.BufferedInputStream in project zaproxy by zaproxy.
the class FileCopier method copyLegacy.
public void copyLegacy(File in, File out) throws IOException {
// CHECKSTYLE:OFF Inner assignments for try-with-resource are okay
try (FileInputStream inStream = new FileInputStream(in);
BufferedInputStream inBuf = new BufferedInputStream(inStream);
FileOutputStream outStream = new FileOutputStream(out);
BufferedOutputStream outBuf = new BufferedOutputStream(outStream)) {
// CHECKSTYLE:ON
byte[] buf = new byte[10240];
int len = 1;
while (len > 0) {
len = inBuf.read(buf);
if (len > 0) {
outBuf.write(buf, 0, len);
}
}
}
}
use of java.io.BufferedInputStream in project xUtils3 by wyouflf.
the class FileLoader method load.
@Override
public File load(final InputStream in) throws Throwable {
File targetFile = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
targetFile = new File(tempSaveFilePath);
if (targetFile.isDirectory()) {
// 防止文件正在写入时, 父文件夹被删除, 继续写入时造成偶现文件节点异常问题.
IOUtil.deleteFileOrDir(targetFile);
}
if (!targetFile.exists()) {
File dir = targetFile.getParentFile();
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("can not create dir: " + dir.getAbsolutePath());
}
}
// 处理[断点逻辑2](见文件头doc)
long targetFileLen = targetFile.length();
if (isAutoResume && targetFileLen > 0) {
FileInputStream fis = null;
try {
long filePos = targetFileLen - CHECK_SIZE;
if (filePos > 0) {
fis = new FileInputStream(targetFile);
byte[] fileCheckBuffer = IOUtil.readBytes(fis, filePos, CHECK_SIZE);
byte[] checkBuffer = IOUtil.readBytes(in, 0, CHECK_SIZE);
if (!Arrays.equals(checkBuffer, fileCheckBuffer)) {
// 先关闭文件流, 否则文件删除会失败.
IOUtil.closeQuietly(fis);
IOUtil.deleteFileOrDir(targetFile);
throw new RuntimeException("need retry");
} else {
contentLength -= CHECK_SIZE;
}
} else {
IOUtil.deleteFileOrDir(targetFile);
throw new RuntimeException("need retry");
}
} finally {
IOUtil.closeQuietly(fis);
}
}
// 开始下载
long current = 0;
FileOutputStream fileOutputStream = null;
if (isAutoResume) {
current = targetFileLen;
fileOutputStream = new FileOutputStream(targetFile, true);
} else {
fileOutputStream = new FileOutputStream(targetFile);
}
long total = contentLength + current;
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(fileOutputStream);
if (progressHandler != null && !progressHandler.updateProgress(total, current, true)) {
throw new Callback.CancelledException("download stopped!");
}
byte[] tmp = new byte[4096];
int len;
while ((len = bis.read(tmp)) != -1) {
// 防止父文件夹被其他进程删除, 继续写入时造成父文件夹变为0字节文件的问题.
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
throw new IOException("parent be deleted!");
}
bos.write(tmp, 0, len);
current += len;
if (progressHandler != null) {
if (!progressHandler.updateProgress(total, current, false)) {
bos.flush();
throw new Callback.CancelledException("download stopped!");
}
}
}
bos.flush();
// 处理[下载逻辑2.a](见文件头doc)
if (diskCacheFile != null) {
targetFile = diskCacheFile.commit();
}
if (progressHandler != null) {
progressHandler.updateProgress(total, current, true);
}
} finally {
IOUtil.closeQuietly(bis);
IOUtil.closeQuietly(bos);
}
return autoRename(targetFile);
}
use of java.io.BufferedInputStream in project xUtils3 by wyouflf.
the class ImageDecoder method decodeGif.
/**
* 转换文件为Movie, 可用于创建GifDrawable.
*
* @param file
* @param options
* @param cancelable
* @return
* @throws IOException
*/
public static Movie decodeGif(File file, ImageOptions options, Callback.Cancelable cancelable) throws IOException {
{
// check params
if (file == null || !file.exists() || file.length() < 1)
return null;
/*if (options == null) {
options = ImageOptions.DEFAULT; // not use
}
if (options.getMaxWidth() <= 0 || options.getMaxHeight() <= 0) {
options.optimizeMaxSize(null);
}*/
}
InputStream in = null;
try {
if (cancelable != null && cancelable.isCancelled()) {
throw new Callback.CancelledException("cancelled during decode image");
}
int buffSize = 1024 * 16;
in = new BufferedInputStream(new FileInputStream(file), buffSize);
in.mark(buffSize);
Movie movie = Movie.decodeStream(in);
if (movie == null) {
throw new IOException("decode image error");
}
return movie;
} catch (IOException ex) {
throw ex;
} catch (Throwable ex) {
LogUtil.e(ex.getMessage(), ex);
return null;
} finally {
IOUtil.closeQuietly(in);
}
}
use of java.io.BufferedInputStream in project XobotOS by xamarin.
the class GestureStore method load.
public void load(InputStream stream, boolean closeStream) throws IOException {
DataInputStream in = null;
try {
in = new DataInputStream((stream instanceof BufferedInputStream) ? stream : new BufferedInputStream(stream, GestureConstants.IO_BUFFER_SIZE));
long start;
if (PROFILE_LOADING_SAVING) {
start = SystemClock.elapsedRealtime();
}
// Read file format version number
final short versionNumber = in.readShort();
switch(versionNumber) {
case 1:
readFormatV1(in);
break;
}
if (PROFILE_LOADING_SAVING) {
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "Loading gestures library = " + (end - start) + " ms");
}
} finally {
if (closeStream)
GestureUtils.closeStream(in);
}
}
use of java.io.BufferedInputStream in project XobotOS by xamarin.
the class DefaultSSLContextImpl method getTrustManagers.
// TODO javax.net.ssl.trustStoreProvider system property
TrustManager[] getTrustManagers() throws GeneralSecurityException, IOException {
if (TRUST_MANAGERS != null) {
return TRUST_MANAGERS;
}
// find TrustStore, TrustManagers
String keystore = System.getProperty("javax.net.ssl.trustStore");
if (keystore == null) {
return null;
}
String keystorepwd = System.getProperty("javax.net.ssl.trustStorePassword");
char[] pwd = (keystorepwd == null) ? null : keystorepwd.toCharArray();
// TODO Defaults: jssecacerts; cacerts
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(keystore));
ks.load(is, pwd);
} finally {
if (is != null) {
is.close();
}
}
String tmfAlg = Security.getProperty("ssl.TrustManagerFactory.algorithm");
if (tmfAlg == null) {
tmfAlg = "PKIX";
}
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlg);
tmf.init(ks);
TRUST_MANAGERS = tmf.getTrustManagers();
return TRUST_MANAGERS;
}
Aggregations