Search in sources :

Example 71 with BufferedOutputStream

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);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 72 with BufferedOutputStream

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);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) Format(org.jdom2.output.Format) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) BufferedOutputStream(java.io.BufferedOutputStream)

Example 73 with BufferedOutputStream

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);
            }
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 74 with BufferedOutputStream

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);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) DiskCacheFile(org.xutils.cache.DiskCacheFile) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 75 with BufferedOutputStream

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);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1219 FileOutputStream (java.io.FileOutputStream)861 IOException (java.io.IOException)617 File (java.io.File)519 OutputStream (java.io.OutputStream)350 BufferedInputStream (java.io.BufferedInputStream)238 InputStream (java.io.InputStream)166 DataOutputStream (java.io.DataOutputStream)158 FileInputStream (java.io.FileInputStream)145 ZipOutputStream (java.util.zip.ZipOutputStream)121 FileNotFoundException (java.io.FileNotFoundException)113 ZipEntry (java.util.zip.ZipEntry)108 ByteArrayOutputStream (java.io.ByteArrayOutputStream)101 ZipFile (java.util.zip.ZipFile)62 URL (java.net.URL)57 XmlSerializer (org.xmlpull.v1.XmlSerializer)57 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)56 ObjectOutputStream (java.io.ObjectOutputStream)54 GZIPOutputStream (java.util.zip.GZIPOutputStream)51 PrintStream (java.io.PrintStream)46