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:Main.java

private static void copyWithStreams(File aSourceFile, File aTargetFile) {
    InputStream inStream = null;/*from   w w  w .  java2s .c  o  m*/
    OutputStream outStream = null;

    try {
        try {
            byte[] bucket = new byte[32 * 1024];
            inStream = new BufferedInputStream(new FileInputStream(aSourceFile));
            outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, false));
            int bytesRead = 0;
            while (bytesRead != -1) {
                bytesRead = inStream.read(bucket); //-1, 0, or more
                if (bytesRead > 0) {
                    outStream.write(bucket, 0, bytesRead);
                }
            }
        } finally {
            if (inStream != null)
                inStream.close();
            if (outStream != null)
                outStream.close();
        }
    } catch (FileNotFoundException ex) {
    } catch (IOException ex) {
    }
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

public static void unzipFile(File destFolder, File zipFile) throws IOException {
    ZipInputStream zipStream = null;
    try {/*from ww  w  . jav a 2s.  com*/
        if (!destFolder.exists()) {
            destFolder.mkdirs();
        }

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile));
        zipStream = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zipStream.getNextEntry()) != null) {
            // get output file
            String name = entry.getName();
            if (name.startsWith("/") || name.startsWith("\\"))
                name = name.substring(1);
            File file = new File(destFolder, name);
            // ensure directory exists
            File dir = file.getParentFile();
            if (!dir.exists())
                dir.mkdirs();
            IOUtils.copy(zipStream, new FileOutputStream(file));
        }
    } finally {
        if (zipStream != null)
            zipStream.close();
    }
}

From source file:com.baidu.qa.service.test.util.MysqlDatabaseManager.java

/**
 * ?mysql db??//from  ww  w.j a  va2 s  .  c  om
 * @param dbname
 * @return
 * @throws SQLException
 */
static public Connection getCon(String dbname) throws SQLException {

    String host = "";
    String user = "";
    String database = "";
    String password = "";
    String driverClass = "";
    String useUnicode = "";
    try {
        // ????
        InputStream in = new BufferedInputStream(
                new FileInputStream(ServiceInterfaceCaseTest.CASEPATH + Constant.FILENAME_DB));

        Properties Info = new Properties();
        Info.load(in);

        host = Info.getProperty(dbname + "_DB_Host");
        user = Info.getProperty(dbname + "_DB_User");
        database = Info.getProperty(dbname + "_DB_DataBase");
        password = Info.getProperty(dbname + "_DB_Password");
        driverClass = Info.getProperty(dbname + "_DB_DriverClass");
        useUnicode = Info.getProperty(dbname + "_DB_UseUnicode");
    } catch (Exception e) {
        log.error("[mysql configure file for" + dbname + "error]:", e);
        throw new AssertionError("[get mysql database config error from properties file]");
    }
    if (host == null) {
        log.error("[load configure file for " + dbname + " error]:");
        return null;
    }

    // 
    String url = "jdbc:mysql://" + host.trim() + "/" + ((database != null) ? database.trim() : "")
            + "?useUnicode=" + ((useUnicode != null) ? useUnicode.trim() : "true&characterEncoding=gbk");

    try {
        Class.forName(driverClass);
    } catch (ClassNotFoundException e) {
        log.error("[class not found]:", e);
        throw new AssertionError("[class not found]");
    }
    Connection con = null;
    try {
        con = DriverManager.getConnection(url, user, password);
    } catch (SQLException a) {
        log.error("[mysql connection exception] ", a);
        throw new AssertionError("[mysql connection exception]");
    }

    return con;

}

From source file:com.eryansky.common.utils.io.IoUtils.java

public static String readFileAsString(String filePath) {
    byte[] buffer = new byte[(int) getFile(filePath).length()];
    BufferedInputStream inputStream = null;
    try {//from  w  w  w .j a  va2s .  c  o  m
        inputStream = new BufferedInputStream(new FileInputStream(getFile(filePath)));
        inputStream.read(buffer);
    } catch (Exception e) {
        //      throw new ServiceException("Couldn't read file " + filePath + ": " + e.getMessage());
    } finally {
        IoUtils.closeSilently(inputStream);
    }
    return new String(buffer);
}

From source file:Main.java

public static Object unmarshall(String cntxtPkg, String filename)
        throws JAXBException, SAXException, IOException {
    return unmarshall(cntxtPkg, new BufferedInputStream(new FileInputStream(filename)));
}

From source file:com.textocat.textokit.morph.opencorpora.resource.GramModelDeserializer.java

public static GramModel from(InputStream in, String srcLabel) throws Exception {
    log.info("About to deserialize GramModel from InputStream of {}...", srcLabel);
    long timeBefore = currentTimeMillis();
    InputStream is = new BufferedInputStream(in);
    ObjectInputStream ois = new ObjectInputStream(is);
    GramModel gm;// w  w w  . jav  a  2 s . com
    try {
        gm = (GramModel) ois.readObject();
    } finally {
        IOUtils.closeQuietly(ois);
    }
    log.info("Deserialization of GramModel finished in {} ms", currentTimeMillis() - timeBefore);
    return gm;
}

From source file:Main.java

/** 
 * Return a Node corresponding to the input XML string representation.
 * @param xmlFile XML file./*from  w ww  .jav  a  2s .  co  m*/
 * @return An instance of Node corresponding to the input XML string representation.
 * @throws ParserConfigurationException 
 * @throws SAXException 
 * @throws IOException 
 */
public static Node getXMLNode(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
    InputSource inputSource = new InputSource(new BufferedInputStream(new FileInputStream(xmlFile)));
    return (getXMLNode(inputSource));
}

From source file:es.caib.sgtsic.util.DataHandlers.java

public static DataHandler byteArrayToDataHandler(byte[] arrayByte) {

    InputStream is = new BufferedInputStream(new ByteArrayInputStream(arrayByte));

    String mimetype = "";

    try {//from w  w  w . ja  va  2 s.  com
        mimetype = URLConnection.guessContentTypeFromStream(is);
    } catch (IOException ex) {
        Logger.getLogger(DataHandlers.class.getName()).log(Level.SEVERE, null, ex);
    }

    DataSource dataSource = new ByteArrayDataSource(arrayByte, mimetype);

    return new DataHandler(dataSource);

}

From source file:Main.java

static boolean isJPEG(File file) throws IOException {
    return internalIsJPEG(new DataInputStream(new BufferedInputStream(new FileInputStream(file))));
}

From source file:com.ln.methods.Getcalendar.java

public static void Main() {
    //TODO store file locally and check if file changed == redownload
    try {/*from  ww  w.j  av a 2  s  .  c  om*/

        //Get source
        // System.setProperty("http.agent", "lnrev2");
        URL calendar = new URL(Calendarurl);
        HttpURLConnection getsource = (HttpURLConnection) calendar.openConnection();
        InputStream in = new BufferedInputStream(getsource.getInputStream());
        //Write source
        Writer sw = new StringWriter();
        char[] b = new char[1024];
        Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        int n;
        while ((n = reader.read(b)) != -1) {
            sw.write(b, 0, n);
        }
        //Source == String && Send source for parsing
        Parser.source = sw.toString();
        Betaparser.source = sw.toString();

        //String lol = sw.toString();
        //lol = StringUtils.substringBetween(lol, "<month year=\"2012\" num=\"2\">", "</month>");
        //Parser.parse();

        Betaparser.parse();

    } catch (MalformedURLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.WARNING_MESSAGE);
    } catch (IOException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.WARNING_MESSAGE);
    }
}