Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

In this page you can find the example usage for java.io BufferedInputStream BufferedInputStream.

Prototype

public BufferedInputStream(InputStream in) 

Source Link

Document

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

Usage

From source file:com.qwazr.utils.SerializationUtils.java

/**
 * Read an object from a file using a buffered stream and GZIP compression
 *
 * @param file the destination file/*  w  ww.  j  ava2 s. co  m*/
 * @param <T>  the type of the object
 * @return the deserialized object
 * @throws IOException
 */
public static <T> T deserialize(File file) throws IOException {
    FileInputStream is = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(is);
    GZIPInputStream zis = new GZIPInputStream(bis);
    try {
        return SerializationUtils.deserialize(zis);
    } finally {
        IOUtils.close(zis, bis, is);
    }
}

From source file:cn.guoyukun.spring.utils.FileCharset.java

/**
 * ??GBKUTF-16LEUTF-16BE,UTF-8window???ANSI,Unicode,Unicode big endian,UTF-8
 *
 * @param file/*ww  w.ja  v a 2  s. com*/
 * @return
 */
public static String getCharset(File file) {
    InputStream is = null;
    try {
        is = new FileInputStream(file);
        return getCharset(new BufferedInputStream(is));
    } catch (FileNotFoundException e) {
        return DEFAULT_CHARSET;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.textocat.textokit.commons.io.ProcessIOUtils.java

public static void feedProcessInput(Process proc, File inFile, boolean closeStdIn) throws IOException {
    InputStream in = new BufferedInputStream(openInputStream(inFile));
    feedProcessInput(proc, in, closeStdIn);
}

From source file:Main.java

public static boolean copyFile(File fromFile, File toFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {/*from  w ww.j  av a  2s . c  o  m*/
        FileInputStream is = new FileInputStream(fromFile);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(toFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

public static int upZipFile(File zipFile, String folderPath) throws IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;/*from w ww.  jav  a  2 s. c  o m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            String dirstr = folderPath + ze.getName();
            dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
            File f = new File(dirstr);
            f.mkdirs();
            continue;
        }
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return 0;
}

From source file:Main.java

static public JSONArray loadJSON(String url) {
    HttpURLConnection connection = null;
    JSONArray json = null;/*from   ww  w  .j ava 2s . c o m*/
    InputStream is = null;

    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(5000);

        is = new BufferedInputStream(connection.getInputStream());
        json = new JSONArray(convertStreamToString(is));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (JSONException je) {
        je.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        if (connection != null) {
            connection.disconnect();
        }
    }

    return json;
}

From source file:Main.java

public static Document parseDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    try (InputStream is = new FileInputStream(xmlFile)) {
        BufferedInputStream in = new BufferedInputStream(is);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource(in);
        return builder.parse(source);
    }/*www .  j  a v a 2 s . c  om*/
}

From source file:com.twitter.heron.downloader.Extractor.java

static void extract(InputStream in, Path destination) throws IOException {
    try (final BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
            final GzipCompressorInputStream gzipInputStream = new GzipCompressorInputStream(
                    bufferedInputStream);
            final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzipInputStream)) {
        final String destinationAbsolutePath = destination.toFile().getAbsolutePath();

        TarArchiveEntry entry;//from  www.j  av a  2s .c o m
        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File f = Paths.get(destinationAbsolutePath, entry.getName()).toFile();
                f.mkdirs();
            } else {
                Path fileDestinationPath = Paths.get(destinationAbsolutePath, entry.getName());

                Files.copy(tarInputStream, fileDestinationPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}

From source file:edu.ehu.galan.wiki2wordnet.wikipedia2wordnet.utils.FileUtils.java

/**
* Given a String containing a path to a bz2 compressed file returns a bufferedReader with the
* contents of the file. The first line will contain the file info, Be warned about this!
*
* @param fileIn - the file to be processed
* @return BufferedReader with the contents of the file
* @throws FileNotFoundException//from w w  w .  j av a 2 s.  c  om
* @throws CompressorException
*/
public static BufferedReader getBufferedReaderForBZ2File(String fileIn)
        throws FileNotFoundException, CompressorException {
    FileInputStream fin = new FileInputStream(fileIn);
    BufferedInputStream bis = new BufferedInputStream(fin);
    CompressorInputStream input = new CompressorStreamFactory().createCompressorInputStream(bis);
    BufferedReader br2 = new BufferedReader(new InputStreamReader(input));
    return br2;
}

From source file:Main.java

public static byte[] readByteArray(Context context, String fileName) throws IOException {

    byte[] data;/*ww  w. ja v a2s  .c o m*/
    int c;

    FileInputStream fis = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedInputStream bos = new BufferedInputStream(fis);

    while ((c = bos.read()) != -1) {
        byteArrayOutputStream.write(c);
    }

    data = byteArrayOutputStream.toByteArray();

    bos.close();
    byteArrayOutputStream.close();
    fis.close();

    return data;
}