Search in sources :

Example 91 with BufferedInputStream

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

Example 92 with BufferedInputStream

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);
}
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 93 with BufferedInputStream

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);
    }
}
Also used : Movie(android.graphics.Movie) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Paint(android.graphics.Paint) FileInputStream(java.io.FileInputStream)

Example 94 with BufferedInputStream

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

Example 95 with BufferedInputStream

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1700 FileInputStream (java.io.FileInputStream)854 IOException (java.io.IOException)836 InputStream (java.io.InputStream)707 File (java.io.File)449 BufferedOutputStream (java.io.BufferedOutputStream)228 FileOutputStream (java.io.FileOutputStream)218 DataInputStream (java.io.DataInputStream)168 ByteArrayInputStream (java.io.ByteArrayInputStream)159 FileNotFoundException (java.io.FileNotFoundException)147 URL (java.net.URL)147 ZipEntry (java.util.zip.ZipEntry)123 ByteArrayOutputStream (java.io.ByteArrayOutputStream)112 OutputStream (java.io.OutputStream)100 ZipInputStream (java.util.zip.ZipInputStream)72 GZIPInputStream (java.util.zip.GZIPInputStream)68 ArrayList (java.util.ArrayList)62 HashMap (java.util.HashMap)62 HttpURLConnection (java.net.HttpURLConnection)61 ObjectInputStream (java.io.ObjectInputStream)56