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:cn.edu.bit.whitesail.utils.WhiteSailConfig.java

public void loadConfig() {
    Properties prop = new Properties();
    try {//from  w  w  w .j  a  v a  2  s  . c om
        prop.load(new BufferedInputStream(new FileInputStream("./src/main/config/whitesail.properties")));
        DATA_DIRECTORY = prop.getProperty("dataDirectory");
        DEFAULT_ENCODING = prop.getProperty("defaultEncoding");
    } catch (IOException ex) {
        LOG.fatal("config file load error( missing config file?)");
    }
}

From source file:ResourceServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //get web.xml for display by a servlet
    String file = "/WEB-INF/web.xml";

    URL url = null;//from www  . j  av a  2s  .  c  om
    URLConnection urlConn = null;
    PrintWriter out = null;
    BufferedInputStream buf = null;
    try {
        out = response.getWriter();
        url = getServletContext().getResource(file);
        //set response header
        response.setContentType("text/xml");

        urlConn = url.openConnection();
        //establish connection with URL presenting web.xml
        urlConn.connect();
        buf = new BufferedInputStream(urlConn.getInputStream());
        int readBytes = 0;
        while ((readBytes = buf.read()) != -1)
            out.write(readBytes);
    } catch (MalformedURLException mue) {
        throw new ServletException(mue.getMessage());
    } catch (IOException ioe) {
        throw new ServletException(ioe.getMessage());
    } finally {
        if (out != null)
            out.close();
        if (buf != null)
            buf.close();
    }
}

From source file:edu.smc.mediacommons.modules.Md5Module.java

public static String getSHA1(final File file) {
    String sha1 = null;/*w w  w .jav a  2s . com*/

    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("SHA1");

        InputStream is = new BufferedInputStream(new FileInputStream(file));
        final byte[] buffer = new byte[1024];
        for (int read = 0; (read = is.read(buffer)) != -1;) {
            messageDigest.update(buffer, 0, read);
        }

        // Convert the byte to hex format
        Formatter formatter = new Formatter();
        for (final byte b : messageDigest.digest()) {
            formatter.format("%02x", b);
        }

        sha1 = formatter.toString();
        formatter.close();
        is.close();
    } catch (IOException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {

    }

    return sha1;
}

From source file:com.t3.net.LocalLocation.java

@Override
public InputStream getContent() throws IOException {
    return new BufferedInputStream(new FileInputStream(getFile()));
}

From source file:es.logongas.iothome.agent.storage.Storage.java

public List<Measure> get() {
    List<Measure> measures;
    InputStream inputStream = null;
    try {/*from   w ww .ja va  2  s.  c  o m*/

        inputStream = new BufferedInputStream(new FileInputStream(fileName));
        measures = (List<Measure>) objectMapper.readValue(inputStream, new TypeReference<ArrayList<Measure>>() {
        });

        if (measures == null) {
            measures = new ArrayList<Measure>();
        }

        return measures;
    } catch (FileNotFoundException ex) {
        //Si no est el fichero simplemente retornamos un nuevo array vacio
        return new ArrayList<Measure>();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:Main.java

/** Reads in an XML document from a local file.
 *  @param file The file to read in.//from   w  w w .jav  a 2 s . c  om
 *  @return The parsed XML document.
 *  @throws ParserConfigurationException If there is an error in the default
 *                                       XML parser configuration.
 *  @throws IOException                  If there is an error reading the file.
 *  @throws SAXException                 If there is an error in parsing the file.
 */
public static Document readDocumentXML(File file)
        throws ParserConfigurationException, IOException, SAXException {
    // Note that we are using:
    //    new BufferedInputStream(new FileInputStream(...))
    // rather than:
    //    new FileInputStream(...)
    // as the later version can be extremely slow on some systems.
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    return builder.parse(in);
}

From source file:org.deeplearning4j.cli.api.flags.Properties.java

@Override
public <E> E value(String value) throws Exception {
    File file = new File(value);
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    props.load(bis);//  w ww  .j  a v  a2s  .  c  o m
    bis.close();
    Configuration configuration = new Configuration();
    for (String key : props.stringPropertyNames()) {
        configuration.set(key, props.getProperty(key));
    }
    return (E) configuration;

}

From source file:edu.usf.cutr.manager.IOManagerImpl.java

@Override
public void downloadFile(String url, String path) throws MalformedURLException, IOException {
    BufferedInputStream in = null;
    FileOutputStream fout = null;
    try {//w  w  w. j  a v a  2s. c  om
        in = new BufferedInputStream(new URL(url).openStream());
        fout = new FileOutputStream(path);

        final byte data[] = new byte[1024];
        int count;
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (fout != null) {
            fout.close();
        }
    }
}

From source file:Main.java

/**
 * zips a directory//from  ww  w  . j a  v a  2  s .c  o m
 * @param dir2zip
 * @param zipOut
 * @param zipFileName
 * @throws IOException
 */
private static void zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName) throws IOException {
    File zipDir = new File(dir2zip);
    // get a listing of the directory content
    String[] dirList = zipDir.list();
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    // loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {
        File f = new File(zipDir, dirList[i]);
        if (f.isDirectory()) {
            // if the File object is a directory, call this
            // function again to add its content recursively
            String filePath = f.getPath();
            zipDir(filePath, zipOut, zipFileName);
            // loop again
            continue;
        }

        if (f.getName().equals(zipFileName)) {
            continue;
        }
        // if we reached here, the File object f was not a directory
        // create a FileInputStream on top of f
        final InputStream fis = new BufferedInputStream(new FileInputStream(f));
        try {
            ZipEntry anEntry = new ZipEntry(f.getPath().substring(dir2zip.length() + 1));
            // place the zip entry in the ZipOutputStream object
            zipOut.putNextEntry(anEntry);
            // now write the content of the file to the ZipOutputStream
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zipOut.write(readBuffer, 0, bytesIn);
            }
        } finally {
            // close the Stream
            fis.close();
        }
    }
}

From source file:Main.java

public static boolean bufferedCopyStream(InputStream inStream, OutputStream outStream, boolean closeStream)
        throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }/*from w ww .  java 2  s  . c  o m*/
        bos.write(data);
    }
    bos.flush();
    if (closeStream) {
        bos.close();
    }
    return true;
}