use of java.io.BufferedOutputStream in project stetho by facebook.
the class FilesDumperPlugin method doDownload.
private void doDownload(PrintStream writer, Iterator<String> remainingArgs) throws DumpUsageException {
String outputPath = ArgsHelper.nextArg(remainingArgs, "Must specify output file or '-'");
ArrayList<File> selectedFiles = new ArrayList<>();
while (remainingArgs.hasNext()) {
selectedFiles.add(resolvePossibleAppStoragePath(mContext, remainingArgs.next()));
}
try {
OutputStream outputStream;
if ("-".equals(outputPath)) {
outputStream = writer;
} else {
outputStream = new FileOutputStream(resolvePossibleSdcardPath(outputPath));
}
ZipOutputStream output = new ZipOutputStream(new BufferedOutputStream(outputStream));
boolean success = false;
try {
byte[] buf = new byte[2048];
if (selectedFiles.size() > 0) {
addFiles(output, buf, selectedFiles.toArray(new File[selectedFiles.size()]));
} else {
addFiles(output, buf, getBaseDir(mContext).listFiles());
}
success = true;
} finally {
try {
output.close();
} catch (IOException e) {
Util.close(outputStream, !success);
if (success) {
throw e;
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of java.io.BufferedOutputStream in project gocd by gocd.
the class GoControlLog method writeLogFile.
protected void writeLogFile(File file, Element element) throws IOException {
// Write the log file out, let jdom care about the encoding by using
// an OutputStream instead of a Writer.
OutputStream logStream = null;
try {
Format format = Format.getPrettyFormat();
XMLOutputter outputter = new XMLOutputter(format);
IO.mkdirFor(file);
file.setWritable(true);
logStream = new BufferedOutputStream(new FileOutputStream(file));
outputter.output(new Document(element), logStream);
} finally {
IO.close(logStream);
}
}
use of java.io.BufferedOutputStream 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.BufferedOutputStream 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.BufferedOutputStream in project XobotOS by xamarin.
the class GestureStore method save.
public void save(OutputStream stream, boolean closeStream) throws IOException {
DataOutputStream out = null;
try {
long start;
if (PROFILE_LOADING_SAVING) {
start = SystemClock.elapsedRealtime();
}
final HashMap<String, ArrayList<Gesture>> maps = mNamedGestures;
out = new DataOutputStream((stream instanceof BufferedOutputStream) ? stream : new BufferedOutputStream(stream, GestureConstants.IO_BUFFER_SIZE));
// Write version number
out.writeShort(FILE_FORMAT_VERSION);
// Write number of entries
out.writeInt(maps.size());
for (Map.Entry<String, ArrayList<Gesture>> entry : maps.entrySet()) {
final String key = entry.getKey();
final ArrayList<Gesture> examples = entry.getValue();
final int count = examples.size();
// Write entry name
out.writeUTF(key);
// Write number of examples for this entry
out.writeInt(count);
for (int i = 0; i < count; i++) {
examples.get(i).serialize(out);
}
}
out.flush();
if (PROFILE_LOADING_SAVING) {
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "Saving gestures library = " + (end - start) + " ms");
}
mChanged = false;
} finally {
if (closeStream)
GestureUtils.closeStream(out);
}
}
Aggregations